In [1]:
%matplotlib widget

import sys
import os
import time
import math as math
from datetime import datetime
import pickle 
import numpy as np
import astropy as ast
from astropy.io import fits
import astropy.io.fits as pyfits
from astropy.table import Table, hstack, vstack, Column
from astropy.stats import sigma_clipped_stats
from astropy.coordinates import Angle
from astropy.time import Time
from photutils.aperture import EllipticalAnnulus, aperture_photometry
import matplotlib
import matplotlib.pyplot as plt
import scipy as sp
import scipy.integrate
import scipy.constants
import scipy.special
import scipy.misc
from scipy import integrate
from scipy.optimize import curve_fit
import scipy.interpolate
from scipy.interpolate import CubicSpline
import glob2
import shutil
from IPython.display import clear_output
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.mplot3d import axes3d
from tqdm.notebook import tqdm as tqdm

import cv2

import Utility3_4 as ut

pi = sp.constants.pi
k= sp.constants.k
q_e = sp.constants.e

DimY = 1096
DimX = 1936

Offset = 2 #ADU Blacklevel Offset

#Gain = 2.421
#SigmaGain = 0.243
Gain = 2.398
SigmaGain = 0.069

DisectString = '\1/'
BackSlash = DisectString[0]
ForwardSlash = '/'
In [2]:
#Loading in Master Hots Array
HotsArray = np.loadtxt('SteadyStateDataSets/MasterHotsArray.csv', delimiter=',')

SetNamesList = ['X0_Y0','X0_YM1','X0_YM2','X0_YM3','X0_YP1','X0_YP2','X0_YP3','XM2_Y0','XM2_YM1','XM2_YP1','XP2_Y0','XP2_YM1','XP2_YP1','XP4_Y0']

CoordsDict = {}
FluxFrameDict = {}
ErrFluxFrameDict = {}

#Populating Dictionaries
for SetName in tqdm(SetNamesList):
    #making flux frame and error flux frame dictionaries
    LoadDirectory = 'SteadyStateDataSets/' + SetName + '/'
    #MeanFluxFrame = np.loadtxt(LoadDirectory + 'PrelimMeanFluxFrame' + '.csv', delimiter=',')
    #ErrorMeanFluxFrame = np.loadtxt(LoadDirectory + 'PrelimErrorMeanFluxFrame' + '.csv', delimiter=',')
    #MeanFluxFrame = np.loadtxt(LoadDirectory + 'PrelimMeanFluxFrame2' + '.csv', delimiter=',')
    #ErrorMeanFluxFrame = np.loadtxt(LoadDirectory + 'PrelimErrorMeanFluxFrame2' + '.csv', delimiter=',')
    MeanFluxFrame = np.loadtxt(LoadDirectory + 'PrelimDarkSubMeanFluxFrame4' + '.csv', delimiter=',')
    ErrorMeanFluxFrame = np.loadtxt(LoadDirectory + 'PrelimDarkSubErrorMeanFluxFrame4' + '.csv', delimiter=',')
    
    FluxFrameDict.update({SetName:MeanFluxFrame})
    ErrFluxFrameDict.update({SetName:ErrorMeanFluxFrame})
    
    #making mosaic coordinates dictionary
    x_y = []
    SM1 = 'X'
    for s in SetName:
        if s.isdigit():
            if SM1 == 'P' or s == '0':
                Sign = 1
            if SM1 == 'M':
                Sign = -1
            x_y.append(int(Sign*int(s)))
        if s != 'X':
            SM1 = s
    CoordsDict.update({SetName:np.array(x_y)})
    
#Loading 1st test translation dictionaries
with open('TranslateX_Dict.pkl', 'rb') as f:
    TranslateX_Dict_Test1 = pickle.load(f)
        
with open('TranslateY_Dict.pkl', 'rb') as f:
    TranslateY_Dict_Test1 = pickle.load(f)
    
#Loading in the 1st test (yes, 1st even though it says Test2) gradient descent translation dictionaries
with open('TranslateX_Dict_GD_Test2.pkl', 'rb') as f:
    TranslateX_Dict_GD_Test2 = pickle.load(f)
        
with open('TranslateY_Dict_GD_Test2.pkl', 'rb') as f:
    TranslateY_Dict_GD_Test2 = pickle.load(f)
    
#Loading in the 2st test
with open('TranslateX_Dict_GD2.pkl', 'rb') as f:
    TranslateX_Dict_GD2 = pickle.load(f)
        
with open('TranslateY_Dict_GD2.pkl', 'rb') as f:
    TranslateY_Dict_GD2 = pickle.load(f)
  0%|          | 0/14 [00:00<?, ?it/s]
In [3]:
#Starting the 4th Test Dictionary

TranslateX_Dict_GD4 = {}
TranslateY_Dict_GD4 = {}

Coords = -1*CoordsDict['X0_Y0']
MosaicX = Coords[0]
MosaicY = Coords[1]

TranslateX_Dict_GD4.update({'X0_Y0':int(((MosaicX/2) + 2)*DimX)})
TranslateY_Dict_GD4.update({'X0_Y0':int((MosaicY + 3)*DimY)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)

#Loading in 3rd Test Dictionary
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)
In [4]:
def ROItoPoints(ROI):
    
    x1 = ROI[0]
    y1 = ROI[2]

    x2 = ROI[1]
    y2 = ROI[2]
    
    x3 = ROI[1]
    y3 = ROI[3]
    
    x4 = ROI[0]
    y4 = ROI[3]
    
    x = [x1,x2,x3,x4]
    y = [y1,y2,y3,y4]
    
    return x, y

#Get dx and dy from translate dict
def Recall_dx_dy(SetName1,SetName2,TranslateX_Dict,TranslateY_Dict):
    
    TranslateX0 = TranslateX_Dict[SetName1]
    TranslateY0 = TranslateY_Dict[SetName1]
    
    dx = TranslateX_Dict[SetName2] - TranslateX0
    dy = TranslateY_Dict[SetName2] - TranslateY0
    
    return dx, dy

def CilpAndMake8bit(img,clip_value):
    img_clip = np.clip(img, 0, clip_value)
    img_stretch = np.rint((img_clip/clip_value)*256)
    img_clip = np.clip(img_stretch, 0, 255)
    img_8bit = img_clip.astype('uint8')
    return img_8bit

def StripClipAndMake8bit(img,clip_min,clip_max):
    img_clip = np.clip(img, 0, clip_max)
    img_clip = img_clip - clip_min
    img_stretch = np.rint((img_clip/(clip_max-clip_min))*256)
    img_clip = np.clip(img_stretch, 0, 255)
    img_8bit = img_clip.astype('uint8')
    return img_8bit

def HelpPickROI(SetName1,SetName2,ROI1,HotsArray,dx,dy):
    
    dx = round(dx)
    dy = round(dy)
    
    MeanFluxFrame_1 = FluxFrameDict[SetName1]
    img1 = np.zeros((DimY,DimX))
    img1 =  MeanFluxFrame_1
    img1 = np.where(HotsArray > 0, 0, img1)
    
    MeanFluxFrame_2 = FluxFrameDict[SetName2]
    img2 = np.zeros((DimY,DimX))
    img2 =  MeanFluxFrame_2
    img2 = np.where(HotsArray > 0, 0, img2)
    
    OverlapFrame1 = np.zeros((DimY,DimX))
    OverlapFrame2 = np.zeros((DimY,DimX))
    OverlapHotsArray = np.zeros((DimY,DimX))
    OverlapHotsArray = HotsArray
    
    for xi in range(DimX):
        for yi in range(DimY):
            
            NewXi = xi + dx
            NewYi = yi + dy
            
            #if NewXi < DimX and NewYi < DimY:
            if NewXi < DimX and NewYi < DimY and NewXi > -1 and NewYi > -1:
                
                OverlapFrame1[NewYi,NewXi] = img1[NewYi,NewXi]
                OverlapFrame2[NewYi,NewXi] = img2[yi,xi]
                
                OverlapHotsArray[NewYi,NewXi] = HotsArray[yi,xi]
                
    OverlapFrame1 = np.where(OverlapHotsArray > 0, 0, OverlapFrame1)
    OverlapFrame2 = np.where(OverlapHotsArray > 0, 0, OverlapFrame2)
    
    #ROI2 = [ROI1[0]+dx,ROI1[1]+dx,ROI1[2]+dy,ROI1[3]+dy]
    #ROI2 = [DimX - ROI1[0]+dx,DimX - ROI1[1]+dx,DimY - ROI1[2]+dy,DimY - ROI1[3]+dy]
    ROI2 = [ROI1[0]-dx,ROI1[1]-dx,ROI1[2]-dy,ROI1[3]-dy]
    #ROI2 isn't being calculated properly
    

    xROI1, yROI1 = ROItoPoints(ROI1)
    xROI2, yROI2 = ROItoPoints(ROI2)
    
    matplotlib.rcParams.update(matplotlib.rcParamsDefault)

    f, ax = plt.subplots(4, 1)
    #im1 = ax[0].imshow(OverlapFrame1, cmap='turbo', vmin = 0, vmax = 5)
    im1 = ax[0].imshow(img1, cmap='turbo', vmin = 0, vmax = 5)
    divider = make_axes_locatable(ax[0])
    cax1 = divider.append_axes("right", size="5%", pad=0.15)
    cbar1 = plt.colorbar(im1, cax=cax1)
    cbar1.formatter.set_powerlimits((0, 0))
    ax[0].fill(xROI1, yROI1, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI1")
    #ax[0].fill(xROI2, yROI2, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI1")
    ax[0].set_xlabel('Pixel x')
    ax[0].set_ylabel('Pixel y')
    ax[0].set_title(r"Frame 1 with ROI [$e^{-}/s$]"
                   "\n" r"490.5 eV", loc = 'center')

    #im2 = ax[1].imshow(OverlapFrame2, cmap='turbo', vmin = 0, vmax = 5)
    im2 = ax[1].imshow(img2, cmap='turbo', vmin = 0, vmax = 5)
    divider = make_axes_locatable(ax[1])
    cax2 = divider.append_axes("right", size="5%", pad=0.15)
    cbar2 = plt.colorbar(im2, cax=cax2)
    cbar2.formatter.set_powerlimits((0, 0))
    ax[1].fill(xROI2, yROI2, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI2")
    #ax[1].fill(xROI1, yROI1, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI2")
    ax[1].set_xlabel('Pixel x')
    ax[1].set_ylabel('Pixel y')
    ax[1].set_title(r"Frame 2with ROI [$e^{-}/s$]"
                   "\n" r"490.5 eV", loc = 'center')
    
    im3 = ax[2].imshow(OverlapFrame1, cmap='turbo', vmin = 0, vmax = 5)
    #im1 = ax[0].imshow(img1, cmap='turbo', vmin = 0, vmax = 5)
    divider = make_axes_locatable(ax[2])
    cax3 = divider.append_axes("right", size="5%", pad=0.15)
    cbar3 = plt.colorbar(im3, cax=cax3)
    cbar3.formatter.set_powerlimits((0, 0))
    ax[2].fill(xROI1, yROI1, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI1")
    #ax[0].fill(xROI2, yROI2, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI1")
    ax[2].set_xlabel('Pixel x')
    ax[2].set_ylabel('Pixel y')
    ax[2].set_title(r"Frame 1 Overlap with ROI [$e^{-}/s$]"
                   "\n" r"490.5 eV", loc = 'center')

    im4 = ax[3].imshow(OverlapFrame2, cmap='turbo', vmin = 0, vmax = 5)
    #im2 = ax[1].imshow(img2, cmap='turbo', vmin = 0, vmax = 5)
    divider = make_axes_locatable(ax[3])
    cax4 = divider.append_axes("right", size="5%", pad=0.15)
    cbar4 = plt.colorbar(im4, cax=cax4)
    cbar4.formatter.set_powerlimits((0, 0))
    #ax[1].fill(xROI2, yROI2, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI2")
    ax[3].fill(xROI1, yROI1, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI2")
    ax[3].set_xlabel('Pixel x')
    ax[3].set_ylabel('Pixel y')
    ax[3].set_title(r"Frame 2 Overlap with ROI [$e^{-}/s$]"
                   "\n" r"490.5 eV", loc = 'center')

    plt.subplots_adjust(hspace = 0.35)
    figure = plt.gcf()
    figure.set_size_inches(10, 20)
    plt.show()
        
    if ROI2[0] > -1 and ROI2[1] < DimX + 1 and ROI2[2] > -1 and ROI2[3] < DimY + 1:
        print("Valid combination of dx, dy, and ROI1.")
        print("ROI1:")
        print(ROI1)
        print("ROI2:")
        print(ROI2)
    else:
        print("Invalid combination of dx, dy, and ROI1.")
        print("ROI1:")
        print(ROI1)
        print("ROI2:")
        print(ROI2)
In [5]:
def WeightedQualityFunction(SetName1,SetName2,ROI1,HotsArray,dx,dy,plot1=False,plot2=False,plotResiduals=False,SavePlots=False):
    #Returns the error weighted mean per pixel absolute residual in the ROI given a dx and dy and the ROI in the first image set
    
    dx = round(dx)
    dy = round(dy)
    
    #Making the two images given the set names and zeroing out the hotpixels
    MeanFluxFrame_1 = FluxFrameDict[SetName1]
    img1 = np.zeros((DimY,DimX))
    img1 =  MeanFluxFrame_1
    img1 = np.where(HotsArray > 0, 0, img1)
    
    ErrMeanFluxFrame_1 = ErrFluxFrameDict[SetName1]
    err1 = np.zeros((DimY,DimX))
    err1 = ErrMeanFluxFrame_1
    err1 = np.where(HotsArray > 0, 1, err1)
    err1 = np.where(ErrMeanFluxFrame_1 <= 0, 1, err1)
    
    #ZeroErrFrame1 = np.zeros((DimY,DimX))
    #ZeroErrFrame1 = np.where(ErrMeanFluxFrame_1 <= 0, 1, ZeroErrFrame1)

    MeanFluxFrame_2 = FluxFrameDict[SetName2]
    img2 = np.zeros((DimY,DimX))
    img2 =  MeanFluxFrame_2
    img2 = np.where(HotsArray > 0, 0, img2)
    
    
    MosaicX = CoordsDict[SetName1][0]
    MosaicY = CoordsDict[SetName1][1]
    SetCoord_1 = '(' + str(MosaicX) + ',' + str(MosaicY) + ')'
    
    MosaicX = CoordsDict[SetName2][0]
    MosaicY = CoordsDict[SetName2][1]
    SetCoord_2 = '(' + str(MosaicX) + ',' + str(MosaicY) + ')'
    
    #Define the same ROI in the second frame
    ROI2 = [ROI1[0]-dx,ROI1[1]-dx,ROI1[2]-dy,ROI1[3]-dy]
    
    #Checking that x's and y's are within bounds of new frame for the given dx and dy
    if ROI2[0] > -1 and ROI2[1] < DimX + 1 and ROI2[2] > -1 and ROI2[3] < DimY + 1:
    #if ROI1[0] + dx > -1 and ROI1[1] + dx < DimX + 1 and ROI1[2] + dy > -1 and ROI1[3] + dy < DimY + 1:
        
        #initialize ROI_Frames as zeros arrays
        DimX_ROI = ROI1[1] - ROI1[0]
        DimY_ROI = ROI1[3] - ROI1[2]
        ROI_Frame1 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_Frame2 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_ErrFrame1 = np.zeros((DimY_ROI,DimX_ROI))

        ROI_HotsArray1 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_HotsArray2 = np.zeros((DimY_ROI,DimX_ROI))
        #ROI_ZeroError1 = np.zeros((DimY_ROI,DimX_ROI))
        
        ROI_Frame1 = img1[ROI1[2]:ROI1[3],ROI1[0]:ROI1[1]]
        ROI_Frame2 = img2[ROI2[2]:ROI2[3],ROI2[0]:ROI2[1]]
        ROI_ErrFrame1 = err1[ROI1[2]:ROI1[3],ROI1[0]:ROI1[1]]

        ROI_HotsArray1 = HotsArray[ROI1[2]:ROI1[3],ROI1[0]:ROI1[1]]
        ROI_HotsArray2 = HotsArray[ROI2[2]:ROI2[3],ROI2[0]:ROI2[1]]
        #ROI_ZeroError1 = ZeroErrFrame1[ROI1[2]:ROI1[3],ROI1[0]:ROI1[1]]
        
        Combo_ROI_HotsArray = ROI_HotsArray1 + ROI_HotsArray2 #+ ROI_ZeroError1  added the pixels with 0 err
        
        Combo_ROI_HotsArray = np.where(Combo_ROI_HotsArray > 0, 1, Combo_ROI_HotsArray) #and the pixels with zero error
        
        NumberPixNotHot = (DimX_ROI*DimY_ROI) - np.sum(Combo_ROI_HotsArray) #and the pixels with zero error
        
        #Find the error weighted mean per pixel absolute residual (using errors from frame 1)
        ResidualsFrame = (ROI_Frame1 - ROI_Frame2)
        WeightedResidualsFrame = np.absolute(ROI_Frame1 - ROI_Frame2)/np.absolute(ROI_ErrFrame1)
        WeightedResidualsFrame = np.where(Combo_ROI_HotsArray > 0, 0, WeightedResidualsFrame)
        SumWeightedResiduals = np.sum(WeightedResidualsFrame)
        mean_per_pixel_weighted_residuals = SumWeightedResiduals/NumberPixNotHot
        
        if plot1 != False:
            
            if SavePlots == True:
                
                SMALL_SIZE = 15
                MEDIUM_SIZE = 15
                BIGGER_SIZE = 20

                plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
                plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
                plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
                plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
                plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
                plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
                plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title
                
            if SavePlots == False:
                matplotlib.rcParams.update(matplotlib.rcParamsDefault)
            
            xROI1, yROI1 = ROItoPoints(ROI1)
            xROI2, yROI2 = ROItoPoints(ROI2)
            
            f, ax = plt.subplots(2, 1)
            im1 = ax[0].imshow(img1, cmap='turbo', vmin = 0, vmax = 5)
            divider = make_axes_locatable(ax[0])
            cax1 = divider.append_axes("right", size="5%", pad=0.15)
            cbar1 = plt.colorbar(im1, cax=cax1)
            cbar1.formatter.set_powerlimits((0, 0))
            ax[0].fill(xROI1, yROI1, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI1")
            ax[0].set_xlabel('Pixel x')
            ax[0].set_ylabel('Pixel y')
            ax[0].set_title(r"Frame 1 " + SetCoord_1 + " with ROI [$e^{-}/s$]"
                           "\n" r"490.5 eV", loc = 'center')

            im2 = ax[1].imshow(img2, cmap='turbo', vmin = 0, vmax = 5)
            divider = make_axes_locatable(ax[1])
            cax2 = divider.append_axes("right", size="5%", pad=0.15)
            cbar2 = plt.colorbar(im2, cax=cax2)
            cbar2.formatter.set_powerlimits((0, 0))
            ax[1].fill(xROI2, yROI2, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI2")
            ax[1].set_xlabel('Pixel x')
            ax[1].set_ylabel('Pixel y')
            ax[1].set_title(r"Frame 2 " + SetCoord_2 + " with ROI [$e^{-}/s$]"
                           "\n" r"490.5 eV", loc = 'center')
            
            plt.subplots_adjust(hspace = 0.35)
            figure = plt.gcf()
            
            if SavePlots == False:
                figure.set_size_inches(10, 10)
                plt.show()
            
            if SavePlots == True:
                figure.set_size_inches(13, 13)
                SaveString = "FullFramesWithROI_" + SetName1 + "_" + SetName2 + ".png"
                plt.savefig(SaveString, dpi=300,bbox_inches='tight')
                plt.show()
            
        if plot2 != False:
            
            if SavePlots == True:
                
                SMALL_SIZE = 15
                MEDIUM_SIZE = 15
                BIGGER_SIZE = 20

                plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
                plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
                plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
                plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
                plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
                plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
                plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title
                
            if SavePlots == False:
                matplotlib.rcParams.update(matplotlib.rcParamsDefault)
            
            f, ax = plt.subplots(2, 1)
            im1 = ax[0].imshow(ROI_Frame1, cmap='turbo', vmin = 0, vmax = 5)
            divider = make_axes_locatable(ax[0])
            cax1 = divider.append_axes("right", size="5%", pad=0.15)
            cbar1 = plt.colorbar(im1, cax=cax1)
            cbar1.formatter.set_powerlimits((0, 0))
            ax[0].set_xlabel('Pixel x')
            ax[0].set_ylabel('Pixel y')
            ax[0].set_title("ROI from Frame 1 " + SetCoord_1 + " [$e^{-}/s$]490.5 eV")

            im2 = ax[1].imshow(ROI_Frame2, cmap='turbo', vmin = 0, vmax = 5)
            divider = make_axes_locatable(ax[1])
            cax2 = divider.append_axes("right", size="5%", pad=0.15)
            cbar2 = plt.colorbar(im2, cax=cax2)
            cbar2.formatter.set_powerlimits((0, 0))
            ax[1].set_xlabel('Pixel x')
            ax[1].set_ylabel('Pixel y')
            ax[1].set_title("ROI from Frame 2 " + SetCoord_2 + " [$e^{-}/s$]")
            
            plt.subplots_adjust(hspace = 0.35)
            figure = plt.gcf()
            #figure.set_size_inches(10, 6)
            #plt.show()
            
            if SavePlots == False:
                figure.set_size_inches(10, 6)
                plt.show()
            
            if SavePlots == True:
                figure.set_size_inches(13, 7)
                SaveString = "JustROI_" + SetName1 + "_" + SetName2 + ".png"
                plt.savefig(SaveString, dpi=300,bbox_inches='tight')
                plt.show()
            
        if plotResiduals != False:
            
            Vmax = 1000
            
            if SavePlots == True:
                
                SMALL_SIZE = 15
                MEDIUM_SIZE = 15
                BIGGER_SIZE = 20

                plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
                plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
                plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
                plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
                plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
                plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
                plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title
                
            if SavePlots == False:
                matplotlib.rcParams.update(matplotlib.rcParamsDefault)
            
            f, ax = plt.subplots(2, 1)
            im1 = ax[0].imshow(WeightedResidualsFrame, cmap='turbo', vmin = 0, vmax = Vmax)
            #im1 = ax[0].imshow(SquaredResiduals, cmap='coolwarm', vmin = 0, vmax = Vmax)
            divider = make_axes_locatable(ax[0])
            cax1 = divider.append_axes("right", size="5%", pad=0.15)
            cbar1 = plt.colorbar(im1, cax=cax1)
            cbar1.formatter.set_powerlimits((0, 0))
            ax[0].set_xlabel('Pixel x')
            ax[0].set_ylabel('Pixel y')
            ax[0].set_title("ROI Error Weighted Residuals " + SetCoord_1 +" and " + SetCoord_2 + " [$e^{-}/s$]")

            #im2 = ax[1].imshow(ResidualsFrame, cmap='turbo', vmin = -300, vmax = 300) #was 150
            #im2 = ax[1].imshow(ResidualsFrame, cmap='coolwarm', vmin = -300, vmax = 300) #was 150
            im2 = ax[1].imshow(ResidualsFrame, cmap='bwr', vmin = -300, vmax = 300) #was 150
            divider = make_axes_locatable(ax[1])
            cax2 = divider.append_axes("right", size="5%", pad=0.15)
            cbar2 = plt.colorbar(im2, cax=cax2)
            cbar2.formatter.set_powerlimits((0, 0))
            ax[1].set_xlabel('Pixel x')
            ax[1].set_ylabel('Pixel y')
            ax[1].set_title("ROI Residuals " + SetCoord_1 +" and " + SetCoord_2 + " [$e^{-}/s$]")
            
            plt.subplots_adjust(hspace = 0.35)
            figure = plt.gcf()
            #figure.set_size_inches(10, 6)
            #plt.show()
            
            if SavePlots == False:
                figure.set_size_inches(10, 6)
                plt.show()
            
            if SavePlots == True:
                figure.set_size_inches(13, 7)
                SaveString = "Residuals_" + SetName1 + "_" + SetName2 + ".png"
                plt.savefig(SaveString, dpi=300,bbox_inches='tight')
                plt.show()
            
        
        
        return mean_per_pixel_weighted_residuals, WeightedResidualsFrame
    
    else: 
        #Need to make it return something to indicate that the dx's and dy's are incompatable with the chosen ROI1 being in
        #both frames
        print("Invalid combination of dx, dy, and ROI1.")
        
        return False, False
In [20]:
def PlotWeightedQualityFunction(SetName1,SetName2,ROI1,HotsArray,dx,dy,xInches,yInches,ResidualScale,SavePlots=False):
    #Returns the mean per pixel squared residual in the ROI given a dx and dy and the ROI in the first image set
    
    dx = round(dx)
    dy = round(dy)
    
    #Making the two images given the set names and zeroing out the hotpixels
    MeanFluxFrame_1 = FluxFrameDict[SetName1]
    img1 = np.zeros((DimY,DimX))
    img1 =  MeanFluxFrame_1
    img1 = np.where(HotsArray > 0, 0, img1)
    
    ErrMeanFluxFrame_1 = ErrFluxFrameDict[SetName1]
    err1 = np.zeros((DimY,DimX))
    err1 = ErrMeanFluxFrame_1
    err1 = np.where(HotsArray > 0, 1, err1)
    err1 = np.where(ErrMeanFluxFrame_1 <= 0, 1, err1)

    MeanFluxFrame_2 = FluxFrameDict[SetName2]
    img2 = np.zeros((DimY,DimX))
    img2 =  MeanFluxFrame_2
    img2 = np.where(HotsArray > 0, 0, img2)
    
    ErrMeanFluxFrame_2 = ErrFluxFrameDict[SetName2]
    err2 = np.zeros((DimY,DimX))
    err2 = ErrMeanFluxFrame_2
    err2 = np.where(HotsArray > 0, 1, err2)
    err2 = np.where(ErrMeanFluxFrame_2 <= 0, 1, err2)
    
    MosaicX = CoordsDict[SetName1][0]
    MosaicY = CoordsDict[SetName1][1]
    SetCoord_1 = '(' + str(MosaicX) + ',' + str(MosaicY) + ')'
    
    MosaicX = CoordsDict[SetName2][0]
    MosaicY = CoordsDict[SetName2][1]
    SetCoord_2 = '(' + str(MosaicX) + ',' + str(MosaicY) + ')'
    
    #Define the same ROI in the second frame
    ROI2 = [ROI1[0]-dx,ROI1[1]-dx,ROI1[2]-dy,ROI1[3]-dy]
    
    #Checking that x's and y's are within bounds of new frame for the given dx and dy
    if ROI2[0] > -1 and ROI2[1] < DimX + 1 and ROI2[2] > -1 and ROI2[3] < DimY + 1:
    #if ROI1[0] + dx > -1 and ROI1[1] + dx < DimX + 1 and ROI1[2] + dy > -1 and ROI1[3] + dy < DimY + 1:
        
        #initialize ROI_Frames as zeros arrays
        DimX_ROI = ROI1[1] - ROI1[0]
        DimY_ROI = ROI1[3] - ROI1[2]
        ROI_Frame1 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_Frame2 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_ErrFrame1 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_ErrFrame2 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_HotsArray1 = np.zeros((DimY_ROI,DimX_ROI))
        ROI_HotsArray2 = np.zeros((DimY_ROI,DimX_ROI))
        
        ROI_Frame1 = img1[ROI1[2]:ROI1[3],ROI1[0]:ROI1[1]]
        ROI_Frame2 = img2[ROI2[2]:ROI2[3],ROI2[0]:ROI2[1]]
        ROI_ErrFrame1 = err1[ROI1[2]:ROI1[3],ROI1[0]:ROI1[1]]
        ROI_ErrFrame1 = err2[ROI2[2]:ROI2[3],ROI2[0]:ROI2[1]]
        ROI_HotsArray1 = HotsArray[ROI1[2]:ROI1[3],ROI1[0]:ROI1[1]]
        ROI_HotsArray2 = HotsArray[ROI2[2]:ROI2[3],ROI2[0]:ROI2[1]]
        
        Combo_ROI_HotsArray = ROI_HotsArray1 + ROI_HotsArray2
        
        Combo_ROI_HotsArray = np.where(Combo_ROI_HotsArray > 0, 1, Combo_ROI_HotsArray)
        
        NumberPixNotHot = (DimX_ROI*DimY_ROI) - np.sum(Combo_ROI_HotsArray)
        
        #Find the error weighted mean per pixel absolute residual (using errors from frame 1)
        ResidualsFrame = (ROI_Frame1 - ROI_Frame2)
        WeightedResidualsFrame = np.absolute(ROI_Frame1 - ROI_Frame2)/np.absolute(ROI_ErrFrame1)
        WeightedResidualsFrame = np.where(Combo_ROI_HotsArray > 0, 0, WeightedResidualsFrame)
        SumWeightedResiduals = np.sum(WeightedResidualsFrame)
        mean_per_pixel_weighted_residuals = SumWeightedResiduals/NumberPixNotHot
            
        if SavePlots == True:

            SMALL_SIZE = 15
            MEDIUM_SIZE = 15
            BIGGER_SIZE = 20

            plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
            plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
            plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
            plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
            plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
            plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
            plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

        if SavePlots == False:
            matplotlib.rcParams.update(matplotlib.rcParamsDefault)

        xROI1, yROI1 = ROItoPoints(ROI1)
        xROI2, yROI2 = ROItoPoints(ROI2)

        f, ax = plt.subplots(2, 1)
        im1 = ax[0].imshow(img1, cmap='turbo', vmin = 0, vmax = 5)
        divider = make_axes_locatable(ax[0])
        cax1 = divider.append_axes("right", size="5%", pad=0.15)
        cbar1 = plt.colorbar(im1, cax=cax1)
        cbar1.formatter.set_powerlimits((0, 0))
        ax[0].fill(xROI1, yROI1, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI1")
        ax[0].set_xlabel('Pixel x')
        ax[0].set_ylabel('Pixel y')
        ax[0].set_title(r"Frame 1 " + SetCoord_1 + " with ROI [$e^{-}/s$]"
                       "\n" r"490.5 eV", loc = 'center')

        im2 = ax[1].imshow(img2, cmap='turbo', vmin = 0, vmax = 5)
        divider = make_axes_locatable(ax[1])
        cax2 = divider.append_axes("right", size="5%", pad=0.15)
        cbar2 = plt.colorbar(im2, cax=cax2)
        cbar2.formatter.set_powerlimits((0, 0))
        ax[1].fill(xROI2, yROI2, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI2")
        ax[1].set_xlabel('Pixel x')
        ax[1].set_ylabel('Pixel y')
        ax[1].set_title(r"Frame 2 " + SetCoord_2 + " with ROI [$e^{-}/s$]"
                       "\n" r"490.5 eV", loc = 'center')

        plt.subplots_adjust(hspace = 0.35)
        figure = plt.gcf()

        if SavePlots == False:
            figure.set_size_inches(10, 10)
            plt.show()

        if SavePlots == True:
            figure.set_size_inches(13, 13)
            SaveString = "FullFramesWithROI_" + SetName1 + "_and_" + SetName2 + ".png"
            plt.savefig(SaveString, dpi=300,bbox_inches='tight')
            plt.show()
            
        if SavePlots == True:

            SMALL_SIZE = 15
            MEDIUM_SIZE = 15
            BIGGER_SIZE = 20

            plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
            plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
            plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
            plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
            plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
            plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
            plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

        if SavePlots == False:
            matplotlib.rcParams.update(matplotlib.rcParamsDefault)

        f, ax = plt.subplots(2, 1)
        im1 = ax[0].imshow(ROI_Frame1, cmap='turbo', vmin = 0, vmax = 5)
        divider = make_axes_locatable(ax[0])
        cax1 = divider.append_axes("right", size="5%", pad=0.15)
        cbar1 = plt.colorbar(im1, cax=cax1)
        cbar1.formatter.set_powerlimits((0, 0))
        ax[0].set_xlabel('Pixel x')
        ax[0].set_ylabel('Pixel y')
        ax[0].set_title("ROI from Frame 1 " + SetCoord_1 + " [$e^{-}/s$]", y=1.05)

        im2 = ax[1].imshow(ROI_Frame2, cmap='turbo', vmin = 0, vmax = 5)
        divider = make_axes_locatable(ax[1])
        cax2 = divider.append_axes("right", size="5%", pad=0.15)
        cbar2 = plt.colorbar(im2, cax=cax2)
        cbar2.formatter.set_powerlimits((0, 0))
        ax[1].set_xlabel('Pixel x')
        ax[1].set_ylabel('Pixel y')
        ax[1].set_title("ROI from Frame 2 " + SetCoord_2 + " [$e^{-}/s$]", y=1.05)

        plt.subplots_adjust(hspace = 0.35)
        figure = plt.gcf()
        #figure.set_size_inches(10, 6)
        #plt.show()

        if SavePlots == False:
            #figure.set_size_inches(10, 6)
            figure.set_size_inches(xInches, yInches)
            plt.show()

        if SavePlots == True:
            figure.set_size_inches(xInches, yInches)
            SaveString = "JustROI_" + SetName1 + "_and_" + SetName2 + ".png"
            plt.savefig(SaveString, dpi=300,bbox_inches='tight')
            plt.show()
            
        Vmax = 1000

        if SavePlots == True:

            SMALL_SIZE = 15
            MEDIUM_SIZE = 15
            BIGGER_SIZE = 20

            plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
            plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
            plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
            plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
            plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
            plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
            plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

        if SavePlots == False:
            matplotlib.rcParams.update(matplotlib.rcParamsDefault)

        f, ax = plt.subplots(2, 1)
        im1 = ax[0].imshow(WeightedResidualsFrame, cmap='turbo', vmin = 0, vmax = Vmax)
        #im1 = ax[0].imshow(SquaredResiduals, cmap='coolwarm', vmin = 0, vmax = Vmax)
        divider = make_axes_locatable(ax[0])
        cax1 = divider.append_axes("right", size="5%", pad=0.15)
        cbar1 = plt.colorbar(im1, cax=cax1)
        cbar1.formatter.set_powerlimits((0, 0))
        ax[0].set_xlabel('Pixel x')
        ax[0].set_ylabel('Pixel y')
        ax[0].set_title("ROI Error Weighted Residuals " + SetCoord_1 +" and " + SetCoord_2 + " [$e^{-}/s$]", y=1.05)

        #im2 = ax[1].imshow(ResidualsFrame, cmap='turbo', vmin = -300, vmax = 300) #was 150
        #im2 = ax[1].imshow(ResidualsFrame, cmap='coolwarm', vmin = -300, vmax = 300) #was 150
        im2 = ax[1].imshow(ResidualsFrame, cmap='bwr', vmin = -1*ResidualScale, vmax = ResidualScale) #was 150
        divider = make_axes_locatable(ax[1])
        cax2 = divider.append_axes("right", size="5%", pad=0.15)
        cbar2 = plt.colorbar(im2, cax=cax2)
        cbar2.formatter.set_powerlimits((0, 0))
        ax[1].set_xlabel('Pixel x')
        ax[1].set_ylabel('Pixel y')
        ax[1].set_title("ROI Residuals " + SetCoord_1 +" and " + SetCoord_2 + " [$e^{-}/s$]", y=1.05)

        plt.subplots_adjust(hspace = 0.35)
        figure = plt.gcf()
        #figure.set_size_inches(10, 6)
        #plt.show()

        if SavePlots == False:
            #figure.set_size_inches(10, 6)
            figure.set_size_inches(xInches, yInches)
            plt.show()

        if SavePlots == True:
            figure.set_size_inches(xInches, yInches)
            SaveString = "Residuals_" + SetName1 + "_and_" + SetName2 + ".png"
            plt.savefig(SaveString, dpi=300,bbox_inches='tight')
            plt.show()
    
    else: 
        #Need to make it return something to indicate that the dx's and dy's are incompatable with the chosen ROI1 being in
        #both frames
        print("Invalid combination of dx, dy, and ROI1.")
In [7]:
def compute_shift_gd3(SetName1,SetName2,ROI1,HotsArray,dx0,dy0,scooch_dimension,verbose=True):
    
    #Keeping variable names from gd2 even though they now mean different things from what they say.
    
    MeanFluxFrame_1 = FluxFrameDict[SetName1]
    img1 = np.zeros((DimY,DimX))
    img1 =  MeanFluxFrame_1
    img1 = np.where(HotsArray > 0, 0, img1)
    
    MeanFluxFrame_2 = FluxFrameDict[SetName2]
    img2 = np.zeros((DimY,DimX))
    img2 =  MeanFluxFrame_2
    img2 = np.where(HotsArray > 0, 0, img2)
    
    scooch = int((scooch_dimension - 1)/2)
    #scooch = 2
    
    dx = round(dx0)
    dy = round(dy0)
    mean_squared_residual, SquaredResidualsFrame = WeightedQualityFunction(SetName1,SetName2,ROI1,HotsArray,dx,dy)
    #Keeping variable names from gd2 even though they now mean different things from what they say.
    #mean_squared_residual, SquaredResidualsFrame = mean_per_pixel_weighted_residuals, WeightedResidualsFrame in truth
    if mean_squared_residual != False:

        new_translation = (dx,dy)
        new_translations_list = [new_translation]

        mean_residuals = [mean_squared_residual]
        #NumOverlapPixelsList = [NumOverlapPixels]

        prev_min_mean_squared_residual = mean_squared_residual

        min_mean_squared_residual = 10**15 #initalizing a value for abs_residual_sum for the for loop, will not be saved

        while min_mean_squared_residual != prev_min_mean_squared_residual:
        #while min_abs_residual_sum/NumOverlapPixels != prev_min_abs_residual_sum:
            #Will keep going until the previous abs
            for x_increment in range(-1*scooch,scooch + 1):
                for y_increment in range(-1*scooch,scooch + 1):

                    new_dx = dx + x_increment
                    new_dy = dy + y_increment

                    new_translation = (new_dx,new_dy)

                    if new_translation not in new_translations_list: 
                        # if tuple (new_dx,new_dy) not in the associated list of translation tuples
                        mean_squared_residual, SquaredResidualsFrame = WeightedQualityFunction(SetName1,SetName2,ROI1,HotsArray,new_dx,new_dy)
                        if mean_squared_residual != False:
                            mean_residuals.append(mean_squared_residual)
                            #NumOverlapPixelsList.append(NumOverlapPixels)
                            #new_translation = (new_dx,new_dy)
                            new_translations_list.append(new_translation)

                            current_min_mean_squared_residual = min(mean_residuals)

            #Find index of min_abs_residual_sum in list of (abs) residual sums
            min_index = mean_residuals.index(current_min_mean_squared_residual)

            #define a new dx = dx0 and dy = dy0 (of minimum residual sum) here to go through the while loop again
            the_translation_of_min = new_translations_list[min_index]
            dx = the_translation_of_min[0]
            dy = the_translation_of_min[1]

            if verbose == True:
                R_SquaredString  = "{:.4e}".format(current_min_mean_squared_residual)
                print("Best so far: dx = " + str(round(dx)) + " dy = " + str(round(dy)) + " Mean(abs(R)/err) = " + R_SquaredString)

            min_mean_squared_residual = current_min_mean_squared_residual

            if min_mean_squared_residual == prev_min_mean_squared_residual:

                break
            else:
                prev_min_mean_squared_residual = min_mean_squared_residual
                min_mean_squared_residual = 10**15
        
        #Renaming varaibles to correct names before returning them
        mean_weighted_residuals = mean_residuals
        min_mean_weighted_residual = min_mean_squared_residual
        return dx, dy, new_translations_list, mean_weighted_residuals, min_mean_weighted_residual
    
    else:
        return False, False, False, False, False
In [8]:
def compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI1,HotsArray,dx0,dy0,verbose=False):
    #Code to itterate compute_shift_gd3 with different starting points
    
    additions = [0,100,-100,150,-150,200,-200]
    
    dx0_list = []
    dy0_list = []
    dx_list = []
    dy_list = []
    residual_list = []

    for dx_addition in tqdm(additions):
        for dy_addition in additions:
            dx1 = dx0 + dx_addition
            dy1 = dy0 + dy_addition

            #print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))

            dx, dy, new_translations_list, mean_weighted_residuals, min_mean_weighted_residual = compute_shift_gd3(KnownFrame,UnknownFrame,ROI1,HotsArray,dx1,dy1,3,verbose=False)
            
            #print("")

            if dx != False:
                dx_list.append(dx)
                dy_list.append(dy)
                dx0_list.append(dx1)
                dy0_list.append(dy1)

                residual_list.append(min_mean_weighted_residual)
                
                if verbose == True:
                    print("")
                    print("---------------------------------------------------------------------------------------")
                    print("Gradient Descent Starting Point: dx0 = " + str(round(dx1)) + " dy0 = " + str(round(dy1)))
                    R_String  = "{:.4e}".format(min_mean_weighted_residual)
                    #print("Best so far: dx = " + str(round(dx)) + " dy = " + str(round(dy)) + " Mean(abs(R)/err) = " + R_String)
                    print("Best so far: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
                    print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
                    print("---------------------------------------------------------------------------------------")
                    print("")
                
    min_mean_weighted_residual = min(residual_list)
    min_index = residual_list.index(min_mean_weighted_residual)
    
    dx = dx_list[min_index]
    dy = dy_list[min_index]
    
    return dx, dy, min_mean_weighted_residual
In [9]:
#Begin the new Mosaic by aligning X0_Y0 and X0_YM1 gradient descent (using dx0 and dy0 from the previous attempt)
#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
KnownFrame = 'X0_Y0'
UnknownFrame = 'X0_YM1'
dx, dy = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI1=[250,1750,710,1050]

HelpPickROI(KnownFrame,UnknownFrame,ROI1,HotsArray,dx,dy)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[250, 1750, 710, 1050]
ROI2:
[195, 1695, 23, 363]
In [11]:
#Getting the dx0 and dy0 from a previous attempt at gradient descent 
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI1,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")

mean_per_pixel_weighted_residuals, WeightedResidualsFrame = WeightedQualityFunction(KnownFrame,UnknownFrame,ROI1,HotsArray,dx,dy,
                                                                          plot1=True,plot2=True,
                                                                          plotResiduals=True)
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 687
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 587
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 537
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 487
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 155 dy0 = 687
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 155 dy0 = 587
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 155 dy0 = 537
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 155 dy0 = 487
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -45 dy0 = 687
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -45 dy0 = 587
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -45 dy0 = 537
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -45 dy0 = 487
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 205 dy0 = 687
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 205 dy0 = 587
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 205 dy0 = 537
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 205 dy0 = 487
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -95 dy0 = 687
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -95 dy0 = 587
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -95 dy0 = 537
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -95 dy0 = 487
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -145 dy0 = 687
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -145 dy0 = 587
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -145 dy0 = 537
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -145 dy0 = 487
Best so far: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 687
Best Gradient Descent Result: dx = 53 dy = 676
Mean Per Pixel Error Weighted Residual in ROI: 5.8123e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

Figure
Figure
Figure
In [12]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [13]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)

KnownFrame = 'X0_Y0'
UnknownFrame = 'X0_YM1'

#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 13
yInches = 7
ResidualScale = 300

ROI1=[250,1750,710,1050]

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI1,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [32]:
KnownFrame = 'X0_Y0'
UnknownFrame = 'XM2_Y0'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[1450,1870,70,980]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[1450, 1870, 70, 980]
ROI2:
[68, 488, 120, 1030]
In [15]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI1,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1382 dy0 = -50
Best so far: dx = 1382 dy = -49
Mean Per Pixel Error Weighted Residual in ROI: 4.6964e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1382 dy0 = 50
Best so far: dx = 1382 dy = -49
Mean Per Pixel Error Weighted Residual in ROI: 4.6964e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1282 dy0 = -50
Best so far: dx = 1302 dy = -50
Mean Per Pixel Error Weighted Residual in ROI: 7.6056e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1282 dy0 = 50
Best so far: dx = 1382 dy = -49
Mean Per Pixel Error Weighted Residual in ROI: 4.6964e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1232 dy0 = -50
Best so far: dx = 1245 dy = -51
Mean Per Pixel Error Weighted Residual in ROI: 8.3055e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1232 dy0 = 50
Best so far: dx = 1195 dy = -54
Mean Per Pixel Error Weighted Residual in ROI: 8.5134e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1182 dy0 = -50
Best so far: dx = 1193 dy = -55
Mean Per Pixel Error Weighted Residual in ROI: 8.5099e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1182 dy0 = 50
Best so far: dx = 1141 dy = -55
Mean Per Pixel Error Weighted Residual in ROI: 8.6498e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1382 dy0 = -50
Best Gradient Descent Result: dx = 1382 dy = -49
Mean Per Pixel Error Weighted Residual in ROI: 4.6964e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [16]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [33]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 7
yInches = 13
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [34]:
KnownFrame = 'X0_Y0'
UnknownFrame = 'X0_YP1'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[50,1750,50,380]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[50, 1750, 50, 380]
ROI2:
[82, 1782, 722, 1052]
In [27]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -32 dy0 = -672
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -32 dy0 = -572
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -32 dy0 = -522
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -32 dy0 = -472
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -132 dy0 = -672
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -132 dy0 = -572
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -132 dy0 = -522
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -132 dy0 = -472
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -182 dy0 = -672
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -182 dy0 = -572
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -182 dy0 = -522
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -182 dy0 = -472
Best so far: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -32 dy0 = -672
Best Gradient Descent Result: dx = -33 dy = -673
Mean Per Pixel Error Weighted Residual in ROI: 5.0811e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [28]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [35]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 13
yInches = 7
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [37]:
KnownFrame = 'X0_Y0'
UnknownFrame = 'XP2_Y0'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[50,650,120,1040]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[50, 650, 120, 1040]
ROI2:
[1246, 1846, 68, 988]
In [38]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1196 dy0 = 52
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1196 dy0 = -48
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1096 dy0 = 52
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1096 dy0 = -48
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1046 dy0 = 52
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1046 dy0 = -48
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -996 dy0 = 52
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -996 dy0 = -48
Best so far: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1196 dy0 = 52
Best Gradient Descent Result: dx = -1190 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.7774e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [39]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [40]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 7
yInches = 13
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [47]:
KnownFrame = 'X0_YP1'
UnknownFrame = 'X0_YP2'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[210,1700,50,350]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[210, 1700, 50, 350]
ROI2:
[196, 1686, 728, 1028]
In [48]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 14 dy0 = -678
Best so far: dx = 16 dy = -675
Mean Per Pixel Error Weighted Residual in ROI: 4.0083e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 14 dy0 = -578
Best so far: dx = 16 dy = -577
Mean Per Pixel Error Weighted Residual in ROI: 4.0177e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 14 dy0 = -528
Best so far: dx = 15 dy = -527
Mean Per Pixel Error Weighted Residual in ROI: 4.0685e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 14 dy0 = -478
Best so far: dx = 17 dy = -478
Mean Per Pixel Error Weighted Residual in ROI: 4.1056e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 114 dy0 = -678
Best so far: dx = 24 dy = -645
Mean Per Pixel Error Weighted Residual in ROI: 4.0390e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 114 dy0 = -578
Best so far: dx = 22 dy = -542
Mean Per Pixel Error Weighted Residual in ROI: 4.0502e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 114 dy0 = -528
Best so far: dx = 26 dy = -512
Mean Per Pixel Error Weighted Residual in ROI: 4.0893e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 114 dy0 = -478
Best so far: dx = 30 dy = -485
Mean Per Pixel Error Weighted Residual in ROI: 4.1284e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -86 dy0 = -678
Best so far: dx = 17 dy = -623
Mean Per Pixel Error Weighted Residual in ROI: 4.0219e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -86 dy0 = -578
Best so far: dx = 17 dy = -539
Mean Per Pixel Error Weighted Residual in ROI: 4.0512e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -86 dy0 = -528
Best so far: dx = 17 dy = -487
Mean Per Pixel Error Weighted Residual in ROI: 4.0967e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -86 dy0 = -478
Best so far: dx = 7 dy = -459
Mean Per Pixel Error Weighted Residual in ROI: 4.1654e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 164 dy0 = -678
Best so far: dx = 22 dy = -612
Mean Per Pixel Error Weighted Residual in ROI: 4.0262e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 164 dy0 = -578
Best so far: dx = 35 dy = -522
Mean Per Pixel Error Weighted Residual in ROI: 4.1319e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 164 dy0 = -528
Best so far: dx = 22 dy = -499
Mean Per Pixel Error Weighted Residual in ROI: 4.0899e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 164 dy0 = -478
Best so far: dx = 24 dy = -455
Mean Per Pixel Error Weighted Residual in ROI: 4.1503e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -136 dy0 = -678
Best so far: dx = 16 dy = -577
Mean Per Pixel Error Weighted Residual in ROI: 4.0177e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -136 dy0 = -578
Best so far: dx = 17 dy = -487
Mean Per Pixel Error Weighted Residual in ROI: 4.0967e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -136 dy0 = -528
Best so far: dx = 7 dy = -459
Mean Per Pixel Error Weighted Residual in ROI: 4.1654e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -136 dy0 = -478
Best so far: dx = 11 dy = -414
Mean Per Pixel Error Weighted Residual in ROI: 4.2023e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -186 dy0 = -678
Best so far: dx = 17 dy = -539
Mean Per Pixel Error Weighted Residual in ROI: 4.0512e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -186 dy0 = -578
Best so far: dx = 7 dy = -459
Mean Per Pixel Error Weighted Residual in ROI: 4.1654e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -186 dy0 = -528
Best so far: dx = 11 dy = -414
Mean Per Pixel Error Weighted Residual in ROI: 4.2023e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -186 dy0 = -478
Best so far: dx = 9 dy = -373
Mean Per Pixel Error Weighted Residual in ROI: 4.2537e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 14 dy0 = -678
Best Gradient Descent Result: dx = 16 dy = -675
Mean Per Pixel Error Weighted Residual in ROI: 4.0083e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [49]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [50]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 13
yInches = 7
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [63]:
KnownFrame = 'XP2_Y0'
UnknownFrame = 'XP4_Y0'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

ROI=[75,560,120,1040]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[75, 560, 120, 1040]
ROI2:
[1381, 1866, 71, 991]
In [64]:
#Performing Itterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1306 dy0 = 49
Best so far: dx = -1307 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.0411e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1306 dy0 = -51
Best so far: dx = -1265 dy = 39
Mean Per Pixel Error Weighted Residual in ROI: 4.0998e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1206 dy0 = 49
Best so far: dx = -1207 dy = 43
Mean Per Pixel Error Weighted Residual in ROI: 4.2196e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1206 dy0 = -51
Best so far: dx = -1187 dy = 15
Mean Per Pixel Error Weighted Residual in ROI: 4.3473e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1156 dy0 = 49
Best so far: dx = -1161 dy = 47
Mean Per Pixel Error Weighted Residual in ROI: 4.3672e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1156 dy0 = -51
Best so far: dx = -1145 dy = -11
Mean Per Pixel Error Weighted Residual in ROI: 4.5678e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1106 dy0 = 49
Best so far: dx = -1109 dy = 44
Mean Per Pixel Error Weighted Residual in ROI: 4.5333e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1106 dy0 = -51
Best so far: dx = -1120 dy = 3
Mean Per Pixel Error Weighted Residual in ROI: 4.5373e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -1306 dy0 = 49
Best Gradient Descent Result: dx = -1307 dy = 50
Mean Per Pixel Error Weighted Residual in ROI: 4.0411e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [65]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [66]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 7
yInches = 13
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [71]:
KnownFrame = 'X0_YM1'
UnknownFrame = 'X0_YM2'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[200,1700,750,1080]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[200, 1700, 750, 1080]
ROI2:
[161, 1661, 60, 390]
In [72]:
#Performing Itterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 39 dy0 = 690
Best so far: dx = 38 dy = 691
Mean Per Pixel Error Weighted Residual in ROI: 4.0485e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 39 dy0 = 590
Best so far: dx = 38 dy = 591
Mean Per Pixel Error Weighted Residual in ROI: 4.0706e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 39 dy0 = 540
Best so far: dx = 40 dy = 540
Mean Per Pixel Error Weighted Residual in ROI: 4.1204e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 39 dy0 = 490
Best so far: dx = 44 dy = 491
Mean Per Pixel Error Weighted Residual in ROI: 4.1867e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 139 dy0 = 690
Best so far: dx = 47 dy = 628
Mean Per Pixel Error Weighted Residual in ROI: 4.0901e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 139 dy0 = 590
Best so far: dx = 56 dy = 537
Mean Per Pixel Error Weighted Residual in ROI: 4.1654e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 139 dy0 = 540
Best so far: dx = 52 dy = 505
Mean Per Pixel Error Weighted Residual in ROI: 4.1799e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 139 dy0 = 490
Best so far: dx = 67 dy = 463
Mean Per Pixel Error Weighted Residual in ROI: 4.3039e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -61 dy0 = 690
Best so far: dx = 33 dy = 657
Mean Per Pixel Error Weighted Residual in ROI: 4.0652e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -61 dy0 = 590
Best so far: dx = 34 dy = 568
Mean Per Pixel Error Weighted Residual in ROI: 4.1041e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -61 dy0 = 540
Best so far: dx = 36 dy = 550
Mean Per Pixel Error Weighted Residual in ROI: 4.1162e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -61 dy0 = 490
Best so far: dx = 42 dy = 523
Mean Per Pixel Error Weighted Residual in ROI: 4.1409e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 189 dy0 = 690
Best so far: dx = 44 dy = 598
Mean Per Pixel Error Weighted Residual in ROI: 4.0721e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 189 dy0 = 590
Best so far: dx = 52 dy = 505
Mean Per Pixel Error Weighted Residual in ROI: 4.1799e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 189 dy0 = 540
Best so far: dx = 67 dy = 463
Mean Per Pixel Error Weighted Residual in ROI: 4.3039e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 189 dy0 = 490
Best so far: dx = 57 dy = 447
Mean Per Pixel Error Weighted Residual in ROI: 4.2727e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -111 dy0 = 690
Best so far: dx = 34 dy = 624
Mean Per Pixel Error Weighted Residual in ROI: 4.0693e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -111 dy0 = 590
Best so far: dx = 36 dy = 550
Mean Per Pixel Error Weighted Residual in ROI: 4.1162e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -111 dy0 = 540
Best so far: dx = 37 dy = 524
Mean Per Pixel Error Weighted Residual in ROI: 4.1444e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -111 dy0 = 490
Best so far: dx = 38 dy = 500
Mean Per Pixel Error Weighted Residual in ROI: 4.1794e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -161 dy0 = 690
Best so far: dx = 34 dy = 582
Mean Per Pixel Error Weighted Residual in ROI: 4.0896e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -161 dy0 = 590
Best so far: dx = 36 dy = 533
Mean Per Pixel Error Weighted Residual in ROI: 4.1382e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -161 dy0 = 540
Best so far: dx = 38 dy = 500
Mean Per Pixel Error Weighted Residual in ROI: 4.1794e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -161 dy0 = 490
Best so far: dx = 33 dy = 409
Mean Per Pixel Error Weighted Residual in ROI: 4.3924e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 39 dy0 = 690
Best Gradient Descent Result: dx = 38 dy = 691
Mean Per Pixel Error Weighted Residual in ROI: 4.0485e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [73]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [74]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 13
yInches = 7
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [76]:
KnownFrame = 'X0_YP2'
UnknownFrame = 'X0_YP3'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[210,1700,50,350]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[210, 1700, 50, 350]
ROI2:
[236, 1726, 737, 1037]
In [77]:
#Performing Itterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -26 dy0 = -687
Best so far: dx = -25 dy = -686
Mean Per Pixel Error Weighted Residual in ROI: 4.0293e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -26 dy0 = -587
Best so far: dx = -26 dy = -589
Mean Per Pixel Error Weighted Residual in ROI: 4.2313e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -26 dy0 = -537
Best so far: dx = -26 dy = -539
Mean Per Pixel Error Weighted Residual in ROI: 4.4020e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -26 dy0 = -487
Best so far: dx = -30 dy = -491
Mean Per Pixel Error Weighted Residual in ROI: 4.5596e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 74 dy0 = -687
Best so far: dx = 49 dy = -668
Mean Per Pixel Error Weighted Residual in ROI: 4.7607e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 74 dy0 = -587
Best so far: dx = 17 dy = -581
Mean Per Pixel Error Weighted Residual in ROI: 4.4813e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 74 dy0 = -537
Best so far: dx = 69 dy = -534
Mean Per Pixel Error Weighted Residual in ROI: 4.8208e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 74 dy0 = -487
Best so far: dx = 73 dy = -485
Mean Per Pixel Error Weighted Residual in ROI: 4.7882e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = -687
Best so far: dx = -35 dy = -653
Mean Per Pixel Error Weighted Residual in ROI: 4.0356e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = -587
Best so far: dx = -76 dy = -576
Mean Per Pixel Error Weighted Residual in ROI: 4.4032e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = -537
Best so far: dx = -115 dy = -535
Mean Per Pixel Error Weighted Residual in ROI: 4.6598e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = -487
Best so far: dx = -124 dy = -485
Mean Per Pixel Error Weighted Residual in ROI: 4.6896e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 124 dy0 = -687
Best so far: dx = -25 dy = -664
Mean Per Pixel Error Weighted Residual in ROI: 4.0281e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 124 dy0 = -587
Best so far: dx = 62 dy = -552
Mean Per Pixel Error Weighted Residual in ROI: 4.8028e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 124 dy0 = -537
Best so far: dx = 105 dy = -518
Mean Per Pixel Error Weighted Residual in ROI: 4.9343e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 124 dy0 = -487
Best so far: dx = 122 dy = -485
Mean Per Pixel Error Weighted Residual in ROI: 4.8855e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = -687
Best so far: dx = -47 dy = -644
Mean Per Pixel Error Weighted Residual in ROI: 4.1043e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = -587
Best so far: dx = -167 dy = -580
Mean Per Pixel Error Weighted Residual in ROI: 5.0776e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = -537
Best so far: dx = -163 dy = -527
Mean Per Pixel Error Weighted Residual in ROI: 4.8981e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = -487
Best so far: dx = -167 dy = -480
Mean Per Pixel Error Weighted Residual in ROI: 4.7942e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 174 dy0 = -687
Best so far: dx = 9 dy = -613
Mean Per Pixel Error Weighted Residual in ROI: 4.3587e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 174 dy0 = -587
Best so far: dx = 165 dy = -578
Mean Per Pixel Error Weighted Residual in ROI: 5.4212e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 174 dy0 = -537
Best so far: dx = 153 dy = -510
Mean Per Pixel Error Weighted Residual in ROI: 5.0445e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 174 dy0 = -487
Best so far: dx = 169 dy = -483
Mean Per Pixel Error Weighted Residual in ROI: 4.9639e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -226 dy0 = -687
Best so far: dx = -53 dy = -614
Mean Per Pixel Error Weighted Residual in ROI: 4.1980e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -226 dy0 = -587
Best so far: dx = -163 dy = -527
Mean Per Pixel Error Weighted Residual in ROI: 4.8981e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -226 dy0 = -537
Best so far: dx = -214 dy = -529
Mean Per Pixel Error Weighted Residual in ROI: 5.1375e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -226 dy0 = -487
Best so far: dx = -222 dy = -475
Mean Per Pixel Error Weighted Residual in ROI: 4.9200e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -26 dy0 = -687
Best Gradient Descent Result: dx = -25 dy = -664
Mean Per Pixel Error Weighted Residual in ROI: 4.0281e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [78]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [80]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 13
yInches = 7
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [88]:
KnownFrame = 'X0_YM2'
UnknownFrame = 'X0_YM3'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[210,1700,700,1080]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[210, 1700, 700, 1080]
ROI2:
[185, 1675, 17, 397]
In [89]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("------------------------------- " + KnownFrame + " and " + UnknownFrame + " -------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 25 dy0 = 683
Best so far: dx = 24 dy = 681
Mean Per Pixel Error Weighted Residual in ROI: 3.7364e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 25 dy0 = 583
Best so far: dx = 29 dy = 584
Mean Per Pixel Error Weighted Residual in ROI: 4.1117e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 25 dy0 = 533
Best so far: dx = 29 dy = 534
Mean Per Pixel Error Weighted Residual in ROI: 4.0817e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 25 dy0 = 483
Best so far: dx = 26 dy = 483
Mean Per Pixel Error Weighted Residual in ROI: 4.1115e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 125 dy0 = 683
Best so far: dx = 40 dy = 674
Mean Per Pixel Error Weighted Residual in ROI: 3.8000e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 125 dy0 = 583
Best so far: dx = 72 dy = 561
Mean Per Pixel Error Weighted Residual in ROI: 4.1564e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 125 dy0 = 533
Best so far: dx = 101 dy = 529
Mean Per Pixel Error Weighted Residual in ROI: 4.2706e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 125 dy0 = 483
Best so far: dx = 104 dy = 484
Mean Per Pixel Error Weighted Residual in ROI: 4.2982e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -75 dy0 = 683
Best so far: dx = 22 dy = 675
Mean Per Pixel Error Weighted Residual in ROI: 3.7482e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -75 dy0 = 583
Best so far: dx = 12 dy = 542
Mean Per Pixel Error Weighted Residual in ROI: 4.1382e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -75 dy0 = 533
Best so far: dx = -7 dy = 504
Mean Per Pixel Error Weighted Residual in ROI: 4.2104e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -75 dy0 = 483
Best so far: dx = -49 dy = 475
Mean Per Pixel Error Weighted Residual in ROI: 4.4431e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 175 dy0 = 683
Best so far: dx = 30 dy = 671
Mean Per Pixel Error Weighted Residual in ROI: 3.7527e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 175 dy0 = 583
Best so far: dx = 104 dy = 548
Mean Per Pixel Error Weighted Residual in ROI: 4.2954e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 175 dy0 = 533
Best so far: dx = 85 dy = 521
Mean Per Pixel Error Weighted Residual in ROI: 4.1871e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 175 dy0 = 483
Best so far: dx = 104 dy = 484
Mean Per Pixel Error Weighted Residual in ROI: 4.2982e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -125 dy0 = 683
Best so far: dx = 22 dy = 675
Mean Per Pixel Error Weighted Residual in ROI: 3.7482e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -125 dy0 = 583
Best so far: dx = 16 dy = 522
Mean Per Pixel Error Weighted Residual in ROI: 4.1147e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -125 dy0 = 533
Best so far: dx = -32 dy = 486
Mean Per Pixel Error Weighted Residual in ROI: 4.3373e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -125 dy0 = 483
Best so far: dx = -3 dy = 468
Mean Per Pixel Error Weighted Residual in ROI: 4.2110e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -175 dy0 = 683
Best so far: dx = 22 dy = 675
Mean Per Pixel Error Weighted Residual in ROI: 3.7482e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -175 dy0 = 583
Best so far: dx = -18 dy = 512
Mean Per Pixel Error Weighted Residual in ROI: 4.2713e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -175 dy0 = 533
Best so far: dx = -49 dy = 475
Mean Per Pixel Error Weighted Residual in ROI: 4.4431e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -175 dy0 = 483
Best so far: dx = -129 dy = 449
Mean Per Pixel Error Weighted Residual in ROI: 4.9388e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
------------------------------- X0_YM2 and X0_YM3 -------------------------------
Gradient Descent Starting Point: dx0 = 25 dy0 = 683
Best Gradient Descent Result: dx = 24 dy = 681
Mean Per Pixel Error Weighted Residual in ROI: 3.7364e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [90]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [92]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 13
yInches = 7
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [102]:
KnownFrame = 'X0_YP1'
UnknownFrame = 'XM2_YP1'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[1420,1850,100,1030]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[1420, 1850, 100, 1030]
ROI2:
[47, 477, 145, 1075]
In [103]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("------------------------------- " + KnownFrame + " and " + UnknownFrame + " -------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1373 dy0 = -45
Best so far: dx = 1371 dy = -43
Mean Per Pixel Error Weighted Residual in ROI: 3.7095e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1373 dy0 = 55
Best so far: dx = 1371 dy = 54
Mean Per Pixel Error Weighted Residual in ROI: 3.7444e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1273 dy0 = -45
Best so far: dx = 1274 dy = -43
Mean Per Pixel Error Weighted Residual in ROI: 3.7466e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1273 dy0 = 55
Best so far: dx = 1272 dy = 55
Mean Per Pixel Error Weighted Residual in ROI: 3.8185e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1223 dy0 = -45
Best so far: dx = 1226 dy = -44
Mean Per Pixel Error Weighted Residual in ROI: 3.7994e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1223 dy0 = 55
Best so far: dx = 1231 dy = 52
Mean Per Pixel Error Weighted Residual in ROI: 3.8714e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1173 dy0 = -45
Best so far: dx = 1172 dy = -45
Mean Per Pixel Error Weighted Residual in ROI: 3.8626e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 1173 dy0 = 55
Best so far: dx = 1173 dy = 54
Mean Per Pixel Error Weighted Residual in ROI: 3.9351e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
------------------------------- X0_YP1 and XM2_YP1 -------------------------------
Gradient Descent Starting Point: dx0 = 1373 dy0 = -45
Best Gradient Descent Result: dx = 1371 dy = -43
Mean Per Pixel Error Weighted Residual in ROI: 3.7095e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [104]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [105]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 7
yInches = 13
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [111]:
KnownFrame = 'XP2_Y0'
UnknownFrame = 'XP2_YM1'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[50,1750,700,1080]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[50, 1750, 700, 1080]
ROI2:
[194, 1894, 7, 387]
In [112]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("------------------------------- " + KnownFrame + " and " + UnknownFrame + " -------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -144 dy0 = 693
Best so far: dx = -144 dy = 692
Mean Per Pixel Error Weighted Residual in ROI: 3.8823e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -144 dy0 = 593
Best so far: dx = -144 dy = 592
Mean Per Pixel Error Weighted Residual in ROI: 3.9306e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -144 dy0 = 543
Best so far: dx = -142 dy = 544
Mean Per Pixel Error Weighted Residual in ROI: 4.0053e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -144 dy0 = 493
Best so far: dx = -145 dy = 494
Mean Per Pixel Error Weighted Residual in ROI: 4.0875e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -44 dy0 = 693
Best so far: dx = -44 dy = 692
Mean Per Pixel Error Weighted Residual in ROI: 3.9240e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -44 dy0 = 593
Best so far: dx = -48 dy = 596
Mean Per Pixel Error Weighted Residual in ROI: 4.0234e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -44 dy0 = 543
Best so far: dx = -48 dy = 546
Mean Per Pixel Error Weighted Residual in ROI: 4.1040e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -44 dy0 = 493
Best so far: dx = -45 dy = 494
Mean Per Pixel Error Weighted Residual in ROI: 4.1903e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 6 dy0 = 693
Best so far: dx = 6 dy = 692
Mean Per Pixel Error Weighted Residual in ROI: 3.9531e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 6 dy0 = 593
Best so far: dx = 6 dy = 595
Mean Per Pixel Error Weighted Residual in ROI: 4.0890e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 6 dy0 = 543
Best so far: dx = 4 dy = 545
Mean Per Pixel Error Weighted Residual in ROI: 4.1679e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 6 dy0 = 493
Best so far: dx = 6 dy = 492
Mean Per Pixel Error Weighted Residual in ROI: 4.2440e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
------------------------------- XP2_Y0 and XP2_YM1 -------------------------------
Gradient Descent Starting Point: dx0 = -144 dy0 = 693
Best Gradient Descent Result: dx = -144 dy = 692
Mean Per Pixel Error Weighted Residual in ROI: 3.8823e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [113]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [114]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 7
yInches = 13
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [116]:
KnownFrame = 'XM2_Y0'
UnknownFrame = 'XM2_YM1'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[50,1700,720,1049]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[50, 1700, 720, 1049]
ROI2:
[26, 1676, 29, 358]
In [117]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("------------------------------- " + KnownFrame + " and " + UnknownFrame + " -------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 24 dy0 = 691
Best so far: dx = 22 dy = 682
Mean Per Pixel Error Weighted Residual in ROI: 3.7127e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 24 dy0 = 591
Best so far: dx = 23 dy = 680
Mean Per Pixel Error Weighted Residual in ROI: 3.7095e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 24 dy0 = 541
Best so far: dx = 28 dy = 549
Mean Per Pixel Error Weighted Residual in ROI: 4.7118e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 24 dy0 = 491
Best so far: dx = 26 dy = 492
Mean Per Pixel Error Weighted Residual in ROI: 4.8240e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -76 dy0 = 691
Best so far: dx = -59 dy = 691
Mean Per Pixel Error Weighted Residual in ROI: 4.1008e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -76 dy0 = 591
Best so far: dx = 1 dy = 678
Mean Per Pixel Error Weighted Residual in ROI: 3.8349e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -76 dy0 = 541
Best so far: dx = -75 dy = 541
Mean Per Pixel Error Weighted Residual in ROI: 4.8953e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -76 dy0 = 491
Best so far: dx = -74 dy = 499
Mean Per Pixel Error Weighted Residual in ROI: 4.9500e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = 691
Best so far: dx = -122 dy = 690
Mean Per Pixel Error Weighted Residual in ROI: 4.2944e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = 591
Best so far: dx = -122 dy = 600
Mean Per Pixel Error Weighted Residual in ROI: 4.7708e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = 541
Best so far: dx = -125 dy = 540
Mean Per Pixel Error Weighted Residual in ROI: 4.9402e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -126 dy0 = 491
Best so far: dx = -125 dy = 492
Mean Per Pixel Error Weighted Residual in ROI: 4.9996e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = 691
Best so far: dx = -170 dy = 692
Mean Per Pixel Error Weighted Residual in ROI: 4.3703e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = 591
Best so far: dx = -142 dy = 667
Mean Per Pixel Error Weighted Residual in ROI: 4.3968e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = 541
Best so far: dx = -174 dy = 543
Mean Per Pixel Error Weighted Residual in ROI: 4.9787e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -176 dy0 = 491
Best so far: dx = -175 dy = 492
Mean Per Pixel Error Weighted Residual in ROI: 5.0385e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
------------------------------- XM2_Y0 and XM2_YM1 -------------------------------
Gradient Descent Starting Point: dx0 = 24 dy0 = 691
Best Gradient Descent Result: dx = 23 dy = 680
Mean Per Pixel Error Weighted Residual in ROI: 3.7095e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [118]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [120]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 13
yInches = 7
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [122]:
KnownFrame = 'XP2_Y0'
UnknownFrame = 'XP2_YP1'

#Begin by using HelpPickROI function to pick a hopefully valid overlaping ROI
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD_Test2,TranslateY_Dict_GD_Test2)

ROI=[50,1680,50,400]

HelpPickROI(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0)
Figure
Valid combination of dx, dy, and ROI1.
ROI1:
[50, 1680, 50, 400]
ROI2:
[263, 1893, 703, 1053]
In [123]:
#Performing Iterated Gradient Descent
dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("------------------------------- " + KnownFrame + " and " + UnknownFrame + " -------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -213 dy0 = -653
Best so far: dx = -212 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 4.6948e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -213 dy0 = -553
Best so far: dx = -212 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 4.6948e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -213 dy0 = -503
Best so far: dx = -212 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 4.6948e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -213 dy0 = -453
Best so far: dx = -222 dy = -460
Mean Per Pixel Error Weighted Residual in ROI: 7.0207e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -113 dy0 = -653
Best so far: dx = -130 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 5.3073e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -113 dy0 = -553
Best so far: dx = -210 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 4.7040e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -113 dy0 = -503
Best so far: dx = -117 dy = -508
Mean Per Pixel Error Weighted Residual in ROI: 7.1294e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -113 dy0 = -453
Best so far: dx = -119 dy = -461
Mean Per Pixel Error Weighted Residual in ROI: 7.2784e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -63 dy0 = -653
Best so far: dx = -130 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 5.3073e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -63 dy0 = -553
Best so far: dx = -210 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 4.7040e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -63 dy0 = -503
Best so far: dx = -94 dy = -536
Mean Per Pixel Error Weighted Residual in ROI: 7.0669e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -63 dy0 = -453
Best so far: dx = -74 dy = -457
Mean Per Pixel Error Weighted Residual in ROI: 7.4136e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -13 dy0 = -653
Best so far: dx = -32 dy = -654
Mean Per Pixel Error Weighted Residual in ROI: 5.8943e+00
---------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -13 dy0 = -553
Best so far: dx = -130 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 5.3073e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -13 dy0 = -503
Best so far: dx = -17 dy = -507
Mean Per Pixel Error Weighted Residual in ROI: 7.4172e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = -13 dy0 = -453
Best so far: dx = -13 dy = -454
Mean Per Pixel Error Weighted Residual in ROI: 7.5892e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
------------------------------- XP2_Y0 and XP2_YP1 -------------------------------
Gradient Descent Starting Point: dx0 = -213 dy0 = -653
Best Gradient Descent Result: dx = -212 dy = -648
Mean Per Pixel Error Weighted Residual in ROI: 4.6948e+00
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

In [124]:
#Updating and saving the Translation Dictionaries with the new Translations.
TranslateX0 = TranslateX_Dict_GD4[KnownFrame]
TranslateY0 = TranslateY_Dict_GD4[KnownFrame]

TranslateX_Dict_GD4.update({UnknownFrame:int(TranslateX0+dx)})
TranslateY_Dict_GD4.update({UnknownFrame:int(TranslateY0+dy)})

with open('TranslateX_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateX_Dict_GD4, f)
        
with open('TranslateY_Dict_GD4.pkl', 'wb') as f:
    pickle.dump(TranslateY_Dict_GD4, f)
In [125]:
#Loading in the new updated and saved Translation Dictionaries 
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_GD4 = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_GD4 = pickle.load(f)


#Ploting and maybe saving QualityFunction function
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD4,TranslateY_Dict_GD4)

xInches = 7
yInches = 13
ResidualScale = 300

PlotWeightedQualityFunction(KnownFrame,UnknownFrame,ROI,HotsArray,dx0,dy0,xInches,yInches,ResidualScale,SavePlots=False)
Figure
Figure
Figure
In [ ]:
 
In [126]:
#Plot the Mosaic

#loading GD dictionary named as test dictionary to avoid having to relablel everywhere
with open('TranslateX_Dict_GD4.pkl', 'rb') as f:
    TranslateX_Dict_Test = pickle.load(f)
        
with open('TranslateY_Dict_GD4.pkl', 'rb') as f:
    TranslateY_Dict_Test = pickle.load(f)
    
#initalizing mosaic
Mosaic = np.zeros((7*DimY,4*DimX))

for SetName in tqdm(SetNamesList):
    MeanFluxFrame = FluxFrameDict[SetName]
    
    Coords = -1*CoordsDict[SetName]
    
    MosaicX = Coords[0]
    MosaicY = Coords[1]
    
    if SetName in TranslateX_Dict_Test:
        TranslateX = TranslateX_Dict_Test[SetName]
        TranslateY = TranslateY_Dict_Test[SetName]
    else:
        TranslateX = int(((MosaicX/2) + 2)*DimX)
        TranslateY = int((MosaicY + 3)*DimY)
    
    for xi in range(DimX):
        for yi in range(DimY):
            NewXi = xi + TranslateX
            NewYi = yi + TranslateY
            
            Mosaic[NewYi,NewXi] = MeanFluxFrame[yi,xi]

            
SMALL_SIZE = 15
MEDIUM_SIZE = 15
BIGGER_SIZE = 20

plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title
            
f, ax = plt.subplots(1, 1)
im1 = ax.imshow(Mosaic, cmap='turbo', vmax=2500)
divider = make_axes_locatable(ax)
cax1 = divider.append_axes("right", size="5%", pad=0.15)
cbar1 = plt.colorbar(im1, cax=cax1)
cbar1.formatter.set_powerlimits((0, 0))
#cbar1.formatter.set_useMathText(True)
for SetName in SetNamesList:
    Coords = -1*CoordsDict[SetName]
    
    MosaicX = Coords[0]
    MosaicY = Coords[1]
    
    if SetName in TranslateX_Dict_Test:
        TranslateX = TranslateX_Dict_Test[SetName]
        TranslateY = TranslateY_Dict_Test[SetName]
    else:
        TranslateX = int(((MosaicX/2) + 2)*DimX)
        TranslateY = int((MosaicY + 3)*DimY)
            
    ROI = [TranslateX, TranslateX + DimX -1, TranslateY, TranslateY + DimY -1]
    xROI, yROI = ROItoPoints(ROI)

    ax.fill(xROI, yROI, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI")
ax.set_xlabel('Pixel x')
ax.set_ylabel('Pixel y')
ax.set_title(r"Mean Flux Mosaic [$e^{-}/s$]"
               "\n" r"490.5 eV", loc = 'center')

plt.subplots_adjust(hspace = 0.35)
figure = plt.gcf()
#figure.set_size_inches(13, 13)
figure.set_size_inches(10, 10)
SaveString = "Mean_Flux_Mosaic_A.png"
#plt.savefig(SaveString, dpi=300,bbox_inches='tight')
#plt.savefig(SaveString, dpi=750,bbox_inches='tight')
plt.show()

#Clipped plot

f, ax = plt.subplots(1, 1)
im1 = ax.imshow(Mosaic, cmap='turbo', vmin = 0, vmax = 20) #vmax used to be 2, 9 is good too
divider = make_axes_locatable(ax)
cax1 = divider.append_axes("right", size="5%", pad=0.15)
cbar1 = plt.colorbar(im1, cax=cax1)
cbar1.formatter.set_powerlimits((0, 0))
#cbar1.formatter.set_useMathText(True)
for SetName in SetNamesList:
    Coords = -1*CoordsDict[SetName]
    
    MosaicX = Coords[0]
    MosaicY = Coords[1]
    
    if SetName in TranslateX_Dict_Test:
        TranslateX = TranslateX_Dict_Test[SetName]
        TranslateY = TranslateY_Dict_Test[SetName]
    else:
        TranslateX = int(((MosaicX/2) + 2)*DimX)
        TranslateY = int((MosaicY + 3)*DimY)
            
    ROI = [TranslateX, TranslateX + DimX -1, TranslateY, TranslateY + DimY -1]
    xROI, yROI = ROItoPoints(ROI)

    ax.fill(xROI, yROI, facecolor='none', edgecolor='magenta', linewidth=2, label = "ROI")
ax.set_xlabel('Pixel x')
ax.set_ylabel('Pixel y')
ax.set_title(r"Mean Flux Mosaic [$e^{-}/s$]"
               "\n" r"490.5 eV", loc = 'center')

plt.subplots_adjust(hspace = 0.35)
figure = plt.gcf()
#figure.set_size_inches(13, 13)
figure.set_size_inches(10, 10)
SaveString = "Mean_Flux_Mosaic_B.png"
#plt.savefig(SaveString, dpi=300,bbox_inches='tight')
#plt.savefig(SaveString, dpi=750,bbox_inches='tight')
plt.show()

#unclipped no boxes
f, ax = plt.subplots(1, 1)
im1 = ax.imshow(Mosaic, cmap='turbo', vmax=2500)
divider = make_axes_locatable(ax)
cax1 = divider.append_axes("right", size="5%", pad=0.15)
cbar1 = plt.colorbar(im1, cax=cax1)
cbar1.formatter.set_powerlimits((0, 0))
ax.set_xlabel('Pixel x')
ax.set_ylabel('Pixel y')
ax.set_title(r"Mean Flux Mosaic [$e^{-}/s$]"
               "\n" r"490.5 eV", loc = 'center')

plt.subplots_adjust(hspace = 0.35)
figure = plt.gcf()
#figure.set_size_inches(13, 13)
figure.set_size_inches(10, 10)
SaveString = "Mean_Flux_Mosaic_C.png"
#plt.savefig(SaveString, dpi=300,bbox_inches='tight')
#plt.savefig(SaveString, dpi=750,bbox_inches='tight')
plt.show()

#Clipped plot, no boxes

f, ax = plt.subplots(1, 1)
im1 = ax.imshow(Mosaic, cmap='turbo', vmin = 0, vmax = 20) #vmax used to be 2
divider = make_axes_locatable(ax)
cax1 = divider.append_axes("right", size="5%", pad=0.15)
cbar1 = plt.colorbar(im1, cax=cax1)
cbar1.formatter.set_powerlimits((0, 0))
#cbar1.formatter.set_useMathText(True)
ax.set_xlabel('Pixel x')
ax.set_ylabel('Pixel y')
ax.set_title(r"Mean Flux Mosaic [$e^{-}/s$]"
               "\n" r"490.5 eV", loc = 'center')

plt.subplots_adjust(hspace = 0.35)
figure = plt.gcf()
#figure.set_size_inches(13, 13)
figure.set_size_inches(10, 10)
SaveString = "Mean_Flux_Mosaic_D.png"
#plt.savefig(SaveString, dpi=300,bbox_inches='tight')
#plt.savefig(SaveString, dpi=750,bbox_inches='tight')
plt.show()
  0%|          | 0/14 [00:00<?, ?it/s]
Figure
Figure
Figure
Figure
In [ ]:
 
In [41]:
ROI1=[250,1750,710,1050]
dx1 = 155
dy1 = 686
compute_shift_gd3(KnownFrame,UnknownFrame,ROI1,HotsArray,dx1,dy1,3,verbose=True)
Best so far: dx = 154 dy = 687 Mean(abs(R)/err) = 1.8447e+01
Best so far: dx = 153 dy = 688 Mean(abs(R)/err) = 1.8106e+01
Best so far: dx = 152 dy = 689 Mean(abs(R)/err) = 1.7774e+01
Best so far: dx = 151 dy = 690 Mean(abs(R)/err) = 1.7447e+01
Best so far: dx = 150 dy = 691 Mean(abs(R)/err) = 1.7120e+01
Best so far: dx = 149 dy = 692 Mean(abs(R)/err) = 1.6812e+01
Best so far: dx = 148 dy = 693 Mean(abs(R)/err) = 1.6504e+01
Best so far: dx = 147 dy = 694 Mean(abs(R)/err) = 1.6202e+01
Best so far: dx = 146 dy = 695 Mean(abs(R)/err) = 1.5912e+01
Best so far: dx = 145 dy = 696 Mean(abs(R)/err) = 1.5631e+01
Best so far: dx = 144 dy = 697 Mean(abs(R)/err) = 1.5362e+01
Best so far: dx = 143 dy = 698 Mean(abs(R)/err) = 1.5099e+01
Best so far: dx = 142 dy = 699 Mean(abs(R)/err) = 1.4842e+01
Best so far: dx = 141 dy = 700 Mean(abs(R)/err) = 1.4594e+01
Best so far: dx = 140 dy = 701 Mean(abs(R)/err) = 1.4358e+01
Best so far: dx = 139 dy = 702 Mean(abs(R)/err) = 1.4129e+01
Best so far: dx = 138 dy = 703 Mean(abs(R)/err) = 1.3914e+01
Best so far: dx = 137 dy = 704 Mean(abs(R)/err) = 1.3705e+01
Best so far: dx = 136 dy = 705 Mean(abs(R)/err) = 1.3507e+01
Best so far: dx = 135 dy = 706 Mean(abs(R)/err) = 1.3308e+01
Best so far: dx = 134 dy = 707 Mean(abs(R)/err) = 1.3128e+01
Best so far: dx = 133 dy = 708 Mean(abs(R)/err) = 1.2944e+01
Best so far: dx = 132 dy = 709 Mean(abs(R)/err) = 1.2771e+01
Best so far: dx = 131 dy = 710 Mean(abs(R)/err) = 1.2609e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 130 dy = 710 Mean(abs(R)/err) = 1.2486e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 129 dy = 710 Mean(abs(R)/err) = 1.2365e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 128 dy = 710 Mean(abs(R)/err) = 1.2244e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 127 dy = 710 Mean(abs(R)/err) = 1.2122e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 126 dy = 710 Mean(abs(R)/err) = 1.2006e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 125 dy = 710 Mean(abs(R)/err) = 1.1887e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 124 dy = 710 Mean(abs(R)/err) = 1.1766e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 123 dy = 710 Mean(abs(R)/err) = 1.1650e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 122 dy = 710 Mean(abs(R)/err) = 1.1533e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 121 dy = 710 Mean(abs(R)/err) = 1.1416e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 120 dy = 710 Mean(abs(R)/err) = 1.1300e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 119 dy = 710 Mean(abs(R)/err) = 1.1183e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 118 dy = 710 Mean(abs(R)/err) = 1.1075e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 117 dy = 710 Mean(abs(R)/err) = 1.0954e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 116 dy = 710 Mean(abs(R)/err) = 1.0844e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 115 dy = 710 Mean(abs(R)/err) = 1.0731e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 114 dy = 710 Mean(abs(R)/err) = 1.0615e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 113 dy = 710 Mean(abs(R)/err) = 1.0501e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 112 dy = 710 Mean(abs(R)/err) = 1.0390e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 111 dy = 710 Mean(abs(R)/err) = 1.0281e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 110 dy = 710 Mean(abs(R)/err) = 1.0172e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 109 dy = 710 Mean(abs(R)/err) = 1.0060e+01
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Best so far: dx = 108 dy = 709 Mean(abs(R)/err) = 9.9518e+00
Best so far: dx = 107 dy = 708 Mean(abs(R)/err) = 9.8392e+00
Best so far: dx = 106 dy = 707 Mean(abs(R)/err) = 9.7285e+00
Best so far: dx = 105 dy = 707 Mean(abs(R)/err) = 9.6189e+00
Best so far: dx = 104 dy = 706 Mean(abs(R)/err) = 9.5130e+00
Best so far: dx = 103 dy = 705 Mean(abs(R)/err) = 9.3973e+00
Best so far: dx = 102 dy = 705 Mean(abs(R)/err) = 9.2892e+00
Best so far: dx = 101 dy = 705 Mean(abs(R)/err) = 9.1813e+00
Best so far: dx = 100 dy = 704 Mean(abs(R)/err) = 9.0677e+00
Best so far: dx = 99 dy = 703 Mean(abs(R)/err) = 8.9583e+00
Best so far: dx = 98 dy = 703 Mean(abs(R)/err) = 8.8516e+00
Best so far: dx = 97 dy = 702 Mean(abs(R)/err) = 8.7421e+00
Best so far: dx = 96 dy = 701 Mean(abs(R)/err) = 8.6316e+00
Best so far: dx = 95 dy = 700 Mean(abs(R)/err) = 8.5096e+00
Best so far: dx = 94 dy = 700 Mean(abs(R)/err) = 8.3982e+00
Best so far: dx = 93 dy = 699 Mean(abs(R)/err) = 8.2852e+00
Best so far: dx = 92 dy = 699 Mean(abs(R)/err) = 8.1636e+00
Best so far: dx = 91 dy = 699 Mean(abs(R)/err) = 8.0490e+00
Best so far: dx = 90 dy = 699 Mean(abs(R)/err) = 7.9424e+00
Best so far: dx = 89 dy = 698 Mean(abs(R)/err) = 7.8274e+00
Best so far: dx = 88 dy = 697 Mean(abs(R)/err) = 7.7094e+00
Best so far: dx = 87 dy = 697 Mean(abs(R)/err) = 7.5937e+00
Best so far: dx = 86 dy = 696 Mean(abs(R)/err) = 7.4795e+00
Best so far: dx = 85 dy = 695 Mean(abs(R)/err) = 7.3602e+00
Best so far: dx = 84 dy = 696 Mean(abs(R)/err) = 7.2406e+00
Best so far: dx = 83 dy = 695 Mean(abs(R)/err) = 7.1300e+00
Best so far: dx = 82 dy = 695 Mean(abs(R)/err) = 7.0186e+00
Best so far: dx = 81 dy = 694 Mean(abs(R)/err) = 6.9075e+00
Best so far: dx = 80 dy = 694 Mean(abs(R)/err) = 6.7915e+00
Best so far: dx = 79 dy = 693 Mean(abs(R)/err) = 6.6922e+00
Best so far: dx = 78 dy = 692 Mean(abs(R)/err) = 6.5846e+00
Best so far: dx = 77 dy = 691 Mean(abs(R)/err) = 6.4854e+00
Best so far: dx = 76 dy = 690 Mean(abs(R)/err) = 6.3884e+00
Best so far: dx = 75 dy = 690 Mean(abs(R)/err) = 6.2944e+00
Best so far: dx = 74 dy = 690 Mean(abs(R)/err) = 6.2050e+00
Best so far: dx = 73 dy = 690 Mean(abs(R)/err) = 6.1163e+00
Best so far: dx = 72 dy = 689 Mean(abs(R)/err) = 6.0389e+00
Best so far: dx = 71 dy = 689 Mean(abs(R)/err) = 5.9582e+00
Best so far: dx = 70 dy = 688 Mean(abs(R)/err) = 5.8843e+00
Best so far: dx = 69 dy = 689 Mean(abs(R)/err) = 5.8117e+00
Best so far: dx = 68 dy = 688 Mean(abs(R)/err) = 5.7463e+00
Best so far: dx = 67 dy = 687 Mean(abs(R)/err) = 5.6849e+00
Best so far: dx = 66 dy = 688 Mean(abs(R)/err) = 5.6240e+00
Best so far: dx = 65 dy = 687 Mean(abs(R)/err) = 5.5718e+00
Best so far: dx = 64 dy = 686 Mean(abs(R)/err) = 5.5242e+00
Best so far: dx = 63 dy = 687 Mean(abs(R)/err) = 5.4909e+00
Best so far: dx = 62 dy = 686 Mean(abs(R)/err) = 5.4529e+00
Best so far: dx = 61 dy = 686 Mean(abs(R)/err) = 5.4301e+00
Best so far: dx = 60 dy = 685 Mean(abs(R)/err) = 5.4102e+00
Best so far: dx = 59 dy = 686 Mean(abs(R)/err) = 5.3970e+00
Best so far: dx = 58 dy = 686 Mean(abs(R)/err) = 5.3915e+00
Best so far: dx = 57 dy = 685 Mean(abs(R)/err) = 5.3867e+00
Best so far: dx = 56 dy = 684 Mean(abs(R)/err) = 5.3856e+00
Best so far: dx = 56 dy = 684 Mean(abs(R)/err) = 5.3856e+00
Out[41]:
(56,
 684,
 [(155, 686),
  (154, 685),
  (154, 686),
  (154, 687),
  (155, 685),
  (155, 687),
  (156, 685),
  (156, 686),
  (156, 687),
  (153, 686),
  (153, 687),
  (153, 688),
  (154, 688),
  (155, 688),
  (152, 687),
  (152, 688),
  (152, 689),
  (153, 689),
  (154, 689),
  (151, 688),
  (151, 689),
  (151, 690),
  (152, 690),
  (153, 690),
  (150, 689),
  (150, 690),
  (150, 691),
  (151, 691),
  (152, 691),
  (149, 690),
  (149, 691),
  (149, 692),
  (150, 692),
  (151, 692),
  (148, 691),
  (148, 692),
  (148, 693),
  (149, 693),
  (150, 693),
  (147, 692),
  (147, 693),
  (147, 694),
  (148, 694),
  (149, 694),
  (146, 693),
  (146, 694),
  (146, 695),
  (147, 695),
  (148, 695),
  (145, 694),
  (145, 695),
  (145, 696),
  (146, 696),
  (147, 696),
  (144, 695),
  (144, 696),
  (144, 697),
  (145, 697),
  (146, 697),
  (143, 696),
  (143, 697),
  (143, 698),
  (144, 698),
  (145, 698),
  (142, 697),
  (142, 698),
  (142, 699),
  (143, 699),
  (144, 699),
  (141, 698),
  (141, 699),
  (141, 700),
  (142, 700),
  (143, 700),
  (140, 699),
  (140, 700),
  (140, 701),
  (141, 701),
  (142, 701),
  (139, 700),
  (139, 701),
  (139, 702),
  (140, 702),
  (141, 702),
  (138, 701),
  (138, 702),
  (138, 703),
  (139, 703),
  (140, 703),
  (137, 702),
  (137, 703),
  (137, 704),
  (138, 704),
  (139, 704),
  (136, 703),
  (136, 704),
  (136, 705),
  (137, 705),
  (138, 705),
  (135, 704),
  (135, 705),
  (135, 706),
  (136, 706),
  (137, 706),
  (134, 705),
  (134, 706),
  (134, 707),
  (135, 707),
  (136, 707),
  (133, 706),
  (133, 707),
  (133, 708),
  (134, 708),
  (135, 708),
  (132, 707),
  (132, 708),
  (132, 709),
  (133, 709),
  (134, 709),
  (131, 708),
  (131, 709),
  (131, 710),
  (132, 710),
  (133, 710),
  (130, 709),
  (130, 710),
  (129, 709),
  (129, 710),
  (128, 709),
  (128, 710),
  (127, 709),
  (127, 710),
  (126, 709),
  (126, 710),
  (125, 709),
  (125, 710),
  (124, 709),
  (124, 710),
  (123, 709),
  (123, 710),
  (122, 709),
  (122, 710),
  (121, 709),
  (121, 710),
  (120, 709),
  (120, 710),
  (119, 709),
  (119, 710),
  (118, 709),
  (118, 710),
  (117, 709),
  (117, 710),
  (116, 709),
  (116, 710),
  (115, 709),
  (115, 710),
  (114, 709),
  (114, 710),
  (113, 709),
  (113, 710),
  (112, 709),
  (112, 710),
  (111, 709),
  (111, 710),
  (110, 709),
  (110, 710),
  (109, 709),
  (109, 710),
  (108, 709),
  (108, 710),
  (107, 708),
  (107, 709),
  (107, 710),
  (108, 708),
  (109, 708),
  (106, 707),
  (106, 708),
  (106, 709),
  (107, 707),
  (108, 707),
  (105, 706),
  (105, 707),
  (105, 708),
  (106, 706),
  (107, 706),
  (104, 706),
  (104, 707),
  (104, 708),
  (103, 705),
  (103, 706),
  (103, 707),
  (104, 705),
  (105, 705),
  (102, 704),
  (102, 705),
  (102, 706),
  (103, 704),
  (104, 704),
  (101, 704),
  (101, 705),
  (101, 706),
  (100, 704),
  (100, 705),
  (100, 706),
  (99, 703),
  (99, 704),
  (99, 705),
  (100, 703),
  (101, 703),
  (98, 702),
  (98, 703),
  (98, 704),
  (99, 702),
  (100, 702),
  (97, 702),
  (97, 703),
  (97, 704),
  (96, 701),
  (96, 702),
  (96, 703),
  (97, 701),
  (98, 701),
  (95, 700),
  (95, 701),
  (95, 702),
  (96, 700),
  (97, 700),
  (94, 699),
  (94, 700),
  (94, 701),
  (95, 699),
  (96, 699),
  (93, 699),
  (93, 700),
  (93, 701),
  (92, 698),
  (92, 699),
  (92, 700),
  (93, 698),
  (94, 698),
  (91, 698),
  (91, 699),
  (91, 700),
  (90, 698),
  (90, 699),
  (90, 700),
  (89, 698),
  (89, 699),
  (89, 700),
  (88, 697),
  (88, 698),
  (88, 699),
  (89, 697),
  (90, 697),
  (87, 696),
  (87, 697),
  (87, 698),
  (88, 696),
  (89, 696),
  (86, 696),
  (86, 697),
  (86, 698),
  (85, 695),
  (85, 696),
  (85, 697),
  (86, 695),
  (87, 695),
  (84, 694),
  (84, 695),
  (84, 696),
  (85, 694),
  (86, 694),
  (83, 695),
  (83, 696),
  (83, 697),
  (84, 697),
  (82, 694),
  (82, 695),
  (82, 696),
  (83, 694),
  (81, 694),
  (81, 695),
  (81, 696),
  (80, 693),
  (80, 694),
  (80, 695),
  (81, 693),
  (82, 693),
  (79, 693),
  (79, 694),
  (79, 695),
  (78, 692),
  (78, 693),
  (78, 694),
  (79, 692),
  (80, 692),
  (77, 691),
  (77, 692),
  (77, 693),
  (78, 691),
  (79, 691),
  (76, 690),
  (76, 691),
  (76, 692),
  (77, 690),
  (78, 690),
  (75, 689),
  (75, 690),
  (75, 691),
  (76, 689),
  (77, 689),
  (74, 689),
  (74, 690),
  (74, 691),
  (73, 689),
  (73, 690),
  (73, 691),
  (72, 689),
  (72, 690),
  (72, 691),
  (71, 688),
  (71, 689),
  (71, 690),
  (72, 688),
  (73, 688),
  (70, 688),
  (70, 689),
  (70, 690),
  (69, 687),
  (69, 688),
  (69, 689),
  (70, 687),
  (71, 687),
  (68, 688),
  (68, 689),
  (68, 690),
  (69, 690),
  (67, 687),
  (67, 688),
  (67, 689),
  (68, 687),
  (66, 686),
  (66, 687),
  (66, 688),
  (67, 686),
  (68, 686),
  (65, 687),
  (65, 688),
  (65, 689),
  (66, 689),
  (64, 686),
  (64, 687),
  (64, 688),
  (65, 686),
  (63, 685),
  (63, 686),
  (63, 687),
  (64, 685),
  (65, 685),
  (62, 686),
  (62, 687),
  (62, 688),
  (63, 688),
  (61, 685),
  (61, 686),
  (61, 687),
  (62, 685),
  (60, 685),
  (60, 686),
  (60, 687),
  (59, 684),
  (59, 685),
  (59, 686),
  (60, 684),
  (61, 684),
  (58, 685),
  (58, 686),
  (58, 687),
  (59, 687),
  (57, 685),
  (57, 686),
  (57, 687),
  (56, 684),
  (56, 685),
  (56, 686),
  (57, 684),
  (58, 684),
  (55, 683),
  (55, 684),
  (55, 685),
  (56, 683),
  (57, 683)],
 [18.786408431762478,
  18.785768180522368,
  18.616259099743573,
  18.446883147153763,
  18.95824187458034,
  18.615506869795908,
  19.134925227035588,
  18.96118095810001,
  18.789418628884903,
  18.444872765378815,
  18.275458390938216,
  18.105658277883652,
  18.279419120902258,
  18.451360832864854,
  18.099477716429664,
  17.930481163269782,
  17.774026046176925,
  17.942936730340524,
  18.121588727442724,
  17.763205667648474,
  17.602252646125116,
  17.44706964174827,
  17.614163299518562,
  17.785602008667404,
  17.431421272982906,
  17.278182248536083,
  17.120179437922996,
  17.294882019214292,
  17.461634721675726,
  17.111867552225945,
  16.958764272011923,
  16.812181740893084,
  16.972367572149132,
  17.143201223644443,
  16.789704792500345,
  16.644487623364952,
  16.504078381463838,
  16.664241949864934,
  16.831449215732313,
  16.48096180175853,
  16.339608794828493,
  16.202351735465903,
  16.365289117586077,
  16.528694400119385,
  16.18074409920487,
  16.04570250091153,
  15.91204891546739,
  16.074760605193315,
  16.235416980274763,
  15.888800555577477,
  15.75640819456916,
  15.630902811882825,
  15.78461875129084,
  15.94161394084572,
  15.601101248212451,
  15.475015169806372,
  15.361727781576905,
  15.507341865727787,
  15.661144296296662,
  15.321859930721265,
  15.204401640613261,
  15.0989395641317,
  15.243462141963557,
  15.39646594246362,
  15.058287672274153,
  14.948646250040147,
  14.8417532490881,
  14.98910055299776,
  15.133938397757154,
  14.799182574931928,
  14.698399602269504,
  14.59441380940493,
  14.744765153486544,
  14.88487030279541,
  14.55431342794209,
  14.453546391015893,
  14.35803874456923,
  14.500263603262505,
  14.646811777403366,
  14.312519179031652,
  14.218194530802329,
  14.12914330245936,
  14.273047594260758,
  14.415887173572367,
  14.082688101155496,
  13.996579824661449,
  13.914206873151453,
  14.04735129569526,
  14.189320799739617,
  13.861362650362251,
  13.778648514927617,
  13.704700670681424,
  13.840570066962306,
  13.970062185536378,
  13.649702443469756,
  13.576935021816878,
  13.50652795163171,
  13.64011194847902,
  13.773580775121566,
  13.44364524231386,
  13.380480452595002,
  13.30826990483514,
  13.439903564889676,
  13.573906637819345,
  13.24415944843153,
  13.179940563960605,
  13.128380748675896,
  13.255697001145183,
  13.37828636578213,
  13.052909320488288,
  12.997710096363743,
  12.943505943788722,
  13.071581774668338,
  13.196677828680667,
  12.871416145467043,
  12.817891549339638,
  12.771229426157634,
  12.895637160050926,
  13.018983608164275,
  12.696728006716588,
  12.652014559604138,
  12.608892337501374,
  12.732522310325482,
  12.851069494317855,
  12.522721451137233,
  12.485840656954332,
  12.403636080485823,
  12.365294693079663,
  12.27608308162524,
  12.244201966532847,
  12.159025231837909,
  12.122163422303911,
  12.041350440738901,
  12.006277454502685,
  11.920420993741649,
  11.88714782583542,
  11.799135153143965,
  11.766352875373933,
  11.681183417588487,
  11.65003993057115,
  11.564132988976292,
  11.533224825515694,
  11.442474668135082,
  11.415773691938943,
  11.321119807784788,
  11.30044889873892,
  11.202783572885588,
  11.183312111804605,
  11.088392617514705,
  11.075216076269065,
  10.970523924968946,
  10.954175130900063,
  10.853572021967352,
  10.844021264658714,
  10.742072304679324,
  10.730832075500041,
  10.624601275310168,
  10.614666446318491,
  10.511668718292272,
  10.501330343433013,
  10.39887369154617,
  10.389630174146749,
  10.286310330793796,
  10.280647991781397,
  10.172316694273158,
  10.172144397097892,
  10.060843429255252,
  10.060006934540887,
  9.951777247305479,
  9.955720817122216,
  9.83921920031149,
  9.843308077785554,
  9.848051467211137,
  9.950077431822036,
  10.060606335623346,
  9.728544120197874,
  9.735751371819607,
  9.735031476146572,
  9.842771549791099,
  9.95361388414495,
  9.61908698454458,
  9.618888676168117,
  9.622272354190246,
  9.735807093974689,
  9.8514758447965,
  9.512995103289601,
  9.517179891869153,
  9.520582964087705,
  9.397329679469673,
  9.404900388232782,
  9.409062098916431,
  9.509150782849325,
  9.629439080123925,
  9.29136436562832,
  9.289191070280058,
  9.297577891540623,
  9.4087332250828,
  9.521094912106491,
  9.18295185027997,
  9.181257642807632,
  9.193046483835488,
  9.067655966958828,
  9.075834481626009,
  9.081753449077926,
  8.958279567516712,
  8.963575709940592,
  8.970470641123402,
  9.072010888183266,
  9.185392824673404,
  8.855372634859744,
  8.851584024063623,
  8.858141705236063,
  8.961415290781165,
  9.081267247654194,
  8.742111015152911,
  8.744796802706915,
  8.748003305643124,
  8.631628672143389,
  8.632821056375885,
  8.634809092408423,
  8.74712887230701,
  8.85580507372089,
  8.509587498820311,
  8.516673071225178,
  8.519145905275122,
  8.631483030995437,
  8.747941709681283,
  8.400766579228739,
  8.398192739068334,
  8.399154854326584,
  8.521826364983145,
  8.637846660143527,
  8.285232821147256,
  8.286251930562333,
  8.289754761229517,
  8.16456642971374,
  8.163631478583559,
  8.168910552651184,
  8.292770399752223,
  8.414165531983372,
  8.05284207327267,
  8.048970049221973,
  8.059173315786582,
  7.9436165543247705,
  7.942409930296011,
  7.95003565965084,
  7.827386189156811,
  7.8296517837692035,
  7.839036746678713,
  7.709413522577665,
  7.71281142105075,
  7.7141547661454375,
  7.825519968177792,
  7.937302229687295,
  7.593810994540168,
  7.593694922587918,
  7.598164874702771,
  7.714384472974736,
  7.831866961157932,
  7.479538400246603,
  7.481423047583996,
  7.488363783689505,
  7.3601896151550665,
  7.364976280599906,
  7.364793407158994,
  7.476143562774238,
  7.6035557986768225,
  7.242253380397212,
  7.243065200767427,
  7.240590314079705,
  7.367515875822445,
  7.483900375229117,
  7.130033253937416,
  7.138288284622589,
  7.149847042308634,
  7.258261308969214,
  7.020122825489857,
  7.018626254534349,
  7.030722936564208,
  7.129133146918415,
  6.9074662619787786,
  6.908190998896536,
  6.9239295524443385,
  6.7925255950887555,
  6.79150120812675,
  6.812644278468072,
  6.907364425087188,
  7.01926496705344,
  6.6921552169532585,
  6.693770869099614,
  6.704114218466022,
  6.584576201522218,
  6.5852952554669875,
  6.59947235262313,
  6.688043873810435,
  6.798851868017924,
  6.485402143642265,
  6.486112837290139,
  6.493789839417633,
  6.58631604561775,
  6.695351493581435,
  6.388377627630028,
  6.3928496646364055,
  6.3930524697451885,
  6.48856085343462,
  6.5995731020040065,
  6.300563348859704,
  6.294417661423472,
  6.297564855810779,
  6.402619578470699,
  6.5068603016092315,
  6.20696394138174,
  6.2049898128051835,
  6.208891599740678,
  6.121858116937521,
  6.116341239886239,
  6.127950905350642,
  6.038904773468793,
  6.039050797967268,
  6.046802695130996,
  5.965092667825066,
  5.9582004943662,
  5.964698392979676,
  6.04062819105642,
  6.132653704544713,
  5.884303443975616,
  5.886752872067544,
  5.887435859990094,
  5.821744103391493,
  5.818065035595187,
  5.8116811609602035,
  5.899440368952818,
  5.972425931088846,
  5.746266025801088,
  5.755102680486798,
  5.7689017687324355,
  5.820443948643968,
  5.684946064005059,
  5.686920609364103,
  5.69163912477462,
  5.750136119732035,
  5.631120283943168,
  5.6273623837561075,
  5.624033458290504,
  5.693202533854028,
  5.764840637602491,
  5.571828420094994,
  5.578099407807991,
  5.593927267151543,
  5.63725624168444,
  5.524208953233174,
  5.525123546590107,
  5.5374013859266755,
  5.579860425011208,
  5.493585268961536,
  5.491964917788766,
  5.490866507993747,
  5.53363081977631,
  5.588581243579451,
  5.45285256830041,
  5.455702109320064,
  5.473423089435656,
  5.500860969309076,
  5.430400854279617,
  5.430104908416554,
  5.439149738899229,
  5.455624201870418,
  5.410190005971206,
  5.411579016090662,
  5.416231356951348,
  5.402084352437619,
  5.399156584674454,
  5.39704549236543,
  5.41878546148671,
  5.443827676664039,
  5.3918349804029875,
  5.391494350768309,
  5.404720936346881,
  5.411466728733839,
  5.386741906909668,
  5.3933558960516,
  5.39938505398883,
  5.385645643903085,
  5.386851495649931,
  5.389074891270665,
  5.391874143841003,
  5.394478641307998,
  5.399875054264605,
  5.391729478413537,
  5.394062334049272,
  5.398324047909641,
  5.402024713752154],
 5.385645643903085)
In [33]:
#Getting the dx0 and dy0 from the previous attempt at gradient descent 
dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD2,TranslateY_Dict_GD2)

dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI1,HotsArray,dx0,dy0,verbose=True)
R_String  = "{:.4e}".format(min_mean_weighted_residual)
print("")
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
print("Best Gradient Descent Result: dx = " + str(round(dx)) + " dy = " + str(round(dy)))
print("Mean Per Pixel Error Weighted Residual in ROI: " + R_String)
print("---------------------------------------------------------------------------------------")
print("---------------------------------------------------------------------------------------")
print("")

mean_per_pixel_weighted_residuals, WeightedResidualsFrame = WeightedQualityFunction(KnownFrame,UnknownFrame,ROI1,HotsArray,dx,dy,
                                                                          plot1=True,plot2=True,
                                                                          plotResiduals=True)
  0%|          | 0/7 [00:00<?, ?it/s]
---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 686
Best so far: dx = 56 dy = 684
Mean Per Pixel Error Weighted Residual in ROI: 5.3856e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 586
Best so far: dx = 56 dy = 684
Mean Per Pixel Error Weighted Residual in ROI: 5.3856e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 536
Best so far: dx = 56 dy = 684
Mean Per Pixel Error Weighted Residual in ROI: 5.3856e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 55 dy0 = 486
Best so far: dx = 56 dy = 684
Mean Per Pixel Error Weighted Residual in ROI: 5.3856e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.
Invalid combination of dx, dy, and ROI1.

---------------------------------------------------------------------------------------
Gradient Descent Starting Point: dx0 = 155 dy0 = 686
Best so far: dx = 130 dy = 711
Mean Per Pixel Error Weighted Residual in ROI: 0.0000e+00
---------------------------------------------------------------------------------------

Invalid combination of dx, dy, and ROI1.
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_12616\144391989.py in <module>
      2 dx0, dy0 = Recall_dx_dy(KnownFrame,UnknownFrame,TranslateX_Dict_GD2,TranslateY_Dict_GD2)
      3 
----> 4 dx, dy, min_mean_weighted_residual = compute_shift_gd_it2(KnownFrame,UnknownFrame,ROI1,HotsArray,dx0,dy0,verbose=True)
      5 R_String  = "{:.4e}".format(min_mean_weighted_residual)
      6 print("")

~\AppData\Local\Temp\ipykernel_12616\2189643564.py in compute_shift_gd_it2(KnownFrame, UnknownFrame, ROI1, HotsArray, dx0, dy0, verbose)
     16             #print("Gradient Descent Starting Point: dx0 = " + str(round(dx0)) + " dy0 = " + str(round(dy0)))
     17 
---> 18             dx, dy, new_translations_list, mean_weighted_residuals, min_mean_weighted_residual = compute_shift_gd3(KnownFrame,UnknownFrame,ROI1,HotsArray,dx1,dy1,3,verbose=False)
     19 
     20             #print("")

~\AppData\Local\Temp\ipykernel_12616\3480698339.py in compute_shift_gd3(SetName1, SetName2, ROI1, HotsArray, dx0, dy0, scooch_dimension, verbose)
     45                     if new_translation not in new_translations_list:
     46                         # if tuple (new_dx,new_dy) not in the associated list of translation tuples
---> 47                         mean_squared_residual, SquaredResidualsFrame = WeightedQualityFunction(SetName1,SetName2,ROI1,HotsArray,new_dx,new_dy)
     48                         mean_residuals.append(mean_squared_residual)
     49                         #NumOverlapPixelsList.append(NumOverlapPixels)

~\AppData\Local\Temp\ipykernel_12616\4242376649.py in WeightedQualityFunction(SetName1, SetName2, ROI1, HotsArray, dx, dy, plot1, plot2, plotResiduals, SavePlots)
     17     err1 = np.where(ErrMeanFluxFrame_1 <= 0, 1, err1)
     18 
---> 19     ZeroErrFrame1 = np.zeros((DimY,DimX))
     20     ZeroErrFrame1 = np.where(ErrMeanFluxFrame_1 <= 0, 1, ZeroErrFrame1)
     21 

KeyboardInterrupt: